C# 对象快速复制
标签:highlight 保存 sha turn ram 复制 readonly htm can
public static class TransExpV2
{
private static readonly Func cache = GetFunc();
private static Func GetFunc()
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
List memberBindingList = new List();
foreach (var item in typeof(TOut).GetProperties())
{
if (!item.CanWrite) continue;
if (typeof(TIn).GetProperty(item.Name) == null) continue;
MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
Expression> lambda = Expression.Lambda>(memberInitExpression, new ParameterExpression[] { parameterExpression });
return lambda.Compile();
}
public static TOut Trans(TIn tIn)
{
return cache(tIn);
}
}
看到一个好的方法保存下来以备用,原文地址:https://www.cnblogs.com/emrys5/p/expression_trans_model.html
C# 对象快速复制
标签:highlight 保存 sha turn ram 复制 readonly htm can
原文地址:https://www.cnblogs.com/chengNet/p/11836536.html
评论