Files
BITFALL/Assets/BITKit/Core/AutoMapper/BITMapper.cs

29 lines
1003 B
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
namespace BITKit
{
public static class BITMapper
{
public static void Map<T>(T source, T target) where T : class
{
2024-08-11 12:46:15 +08:00
foreach (var info in source.GetType().GetProperties(ReflectionHelper.Flags))
2023-08-12 01:43:24 +08:00
{
2024-08-11 12:46:15 +08:00
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 );
}
2023-08-12 01:43:24 +08:00
}
2024-08-11 12:46:15 +08:00
foreach (var info in source.GetType().GetFields(ReflectionHelper.Flags))
2023-08-12 01:43:24 +08:00
{
2024-08-11 12:46:15 +08:00
var targetInfo = target.GetType().GetField(info.Name, ReflectionHelper.Flags);
var value = info.GetValue(source);
if (value is not null)
{
targetInfo?.SetValue(target, value);
}
2023-08-12 01:43:24 +08:00
}
}
}
}