BITKit/Src/Core/AutoMapper/BITMapper.cs

21 lines
775 B
C#
Raw Normal View History

2023-06-29 14:57:11 +08:00
namespace BITKit
2023-06-05 19:57:17 +08:00
{
public static class BITMapper
{
public static void Map<T>(T source, T target) where T : class
{
2024-07-29 14:56:29 +08:00
foreach (var info in source.GetType().GetProperties(ReflectionHelper.Flags))
2023-06-05 19:57:17 +08:00
{
2024-07-29 14:56:29 +08:00
if (info.CanWrite is false) continue;
var targetInfo = target.GetType().GetProperty(info.Name, ReflectionHelper.Flags);
targetInfo?.SetValue(target, info.GetValue(source));
2023-06-05 19:57:17 +08:00
}
2024-07-29 14:56:29 +08:00
foreach (var info in source.GetType().GetFields(ReflectionHelper.Flags))
2023-06-05 19:57:17 +08:00
{
2024-07-29 14:56:29 +08:00
var targetInfo = target.GetType().GetField(info.Name, ReflectionHelper.Flags);
targetInfo?.SetValue(target, info.GetValue(source));
2023-06-05 19:57:17 +08:00
}
}
}
}