mirror of
https://github.com/wagesj45/CapyKit.git
synced 2025-02-05 02:42:59 -06:00
Added property transfer
This commit is contained in:
parent
9acafd8ce6
commit
8682f50500
2 changed files with 53 additions and 1 deletions
|
@ -6,7 +6,7 @@
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<Version>1.0.2</Version>
|
<Version>1.0.3</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
52
CapyKit/Extensions/ObjectExtensions.cs
Normal file
52
CapyKit/Extensions/ObjectExtensions.cs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CapyKit.Extensions
|
||||||
|
{
|
||||||
|
/// <summary> An class containing extenstions that apply to any object type. </summary>
|
||||||
|
public static class ObjectExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An object extension method that updates the properties of a given <paramref name="target"/>
|
||||||
|
/// object with the values from a given <paramref name="source"/> object.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"> Generic type parameter. </typeparam>
|
||||||
|
/// <param name="target"> The target object to act on. </param>
|
||||||
|
/// <param name="source"> Source for the new property values. </param>
|
||||||
|
public static void UpdateProperties<T>(this T target, T source)
|
||||||
|
{
|
||||||
|
var properties = typeof(T).GetProperties();
|
||||||
|
foreach (var prop in properties)
|
||||||
|
{
|
||||||
|
if (prop.CanWrite)
|
||||||
|
{
|
||||||
|
prop.SetValue(target, prop.GetValue(source));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An object extension method that updates the properties of a given <paramref name="target"/>
|
||||||
|
/// object with the values from a given <paramref name="source"/> object.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="target"> The target object to act on. </param>
|
||||||
|
/// <param name="source"> Source for the new property values. </param>
|
||||||
|
public static void UpdateProperties(this object target, object source)
|
||||||
|
{
|
||||||
|
var targetProperties = target.GetType().GetProperties();
|
||||||
|
var sourceProperties = source.GetType().GetProperties();
|
||||||
|
var matchingProperties = targetProperties.Join(sourceProperties, outer => new { outer.Name, outer.PropertyType }, inner => new { inner.Name, inner.PropertyType }, (outer, inner) => new { Target = outer, Source = inner });
|
||||||
|
|
||||||
|
foreach (var propertyMatch in matchingProperties)
|
||||||
|
{
|
||||||
|
if(propertyMatch.Target.CanWrite)
|
||||||
|
{
|
||||||
|
propertyMatch.Target.SetValue(target, propertyMatch.Source.GetValue(source));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue