39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
namespace MasstransferCommon.Utils;
|
|
|
|
/// <summary>
|
|
/// bean 工具类
|
|
/// </summary>
|
|
public class BeanUtil
|
|
{
|
|
/// <summary>
|
|
/// 实现Bean的属性复制
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="target"></param>
|
|
public static void CopyProperties(object source, object target)
|
|
{
|
|
var sourceProperties = source.GetType().GetProperties();
|
|
var targetProperties = target.GetType().GetProperties();
|
|
|
|
foreach (var sourceProperty in sourceProperties)
|
|
{
|
|
var targetProperty = Array.Find(targetProperties, p => p.Name == sourceProperty.Name &&
|
|
p.PropertyType == sourceProperty.PropertyType);
|
|
|
|
if (targetProperty != null && targetProperty.CanWrite)
|
|
targetProperty.SetValue(target, sourceProperty.GetValue(source));
|
|
}
|
|
}
|
|
|
|
public static T CopyProperties<T>(object source)
|
|
{
|
|
var instance = Activator.CreateInstance<T>();
|
|
CopyProperties(source, instance);
|
|
return instance;
|
|
}
|
|
|
|
public static List<T> CopyProperties<T>(IEnumerable<object> source)
|
|
{
|
|
return source.Select(CopyProperties<T>).ToList();
|
|
}
|
|
} |