BITKit/Src/Core/AutoMapper/BITMapper.cs

30 lines
1.0 KiB
C#

namespace BITKit
{
public static class BITMapper
{
public static T Map<T>(object source, T target) where T : class
{
foreach (var info in source.GetType().GetProperties(ReflectionHelper.Flags))
{
if (info.CanWrite is false) continue;
var targetInfo = target.GetType().GetProperty(info.Name, ReflectionHelper.Flags);
var value = info.GetValue(source);
if (value is not null)
{
targetInfo?.SetValue(target,value );
}
}
foreach (var info in source.GetType().GetFields(ReflectionHelper.Flags))
{
var targetInfo = target.GetType().GetField(info.Name, ReflectionHelper.Flags);
var value = info.GetValue(source);
if (value is not null)
{
targetInfo?.SetValue(target, value);
}
}
return target;
}
}
}