BITKit/Src/Core/AutoMapper/BITMapper.cs

29 lines
1003 B
C#

namespace BITKit
{
public static class BITMapper
{
public static void Map<T>(T 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);
}
}
}
}
}