using System;
using System.Collections.Generic;
using System.Text;
namespace Faclon.Extend
{
///
/// Object扩展
///
public static class ObjectExtend
{
///
/// 将当前对象属性赋值到目标对象属性中。
///
/// 当前对象
/// 目标属性
/// 复制选项
public static void CopyTo(this object source,object target,ObjectCopyToOption option = null) {
if(source == null)
throw new ArgumentNullException(nameof(source));
if(target == null)
throw new ArgumentNullException(nameof(target));
option = option ?? ObjectCopyToOption.Default;
foreach(var ps in source.GetType().GetProperties()) {
if(ps.CanRead) {
var pt = target.GetType().GetProperty(ps.Name);
if(pt.CanWrite && ps.PropertyType.IsAssignableFrom(pt.PropertyType)) {
pt.SetValue(target,ps.GetValue(source,null),null);
}
}
}
}
///
/// 根据提供的更新方法更新目标对象属性
///
/// 原对象类型
/// 目标对象类型
/// 原对象
/// 目标对象
/// 转换方法
public static void CopyTo(this TSource source,TTarget target,Action action) {
action(source,target);
}
}
}