使用表达式目录树实现两个不同类型的属性赋值:

public class People
{
public int Age { get; set; }
public string Name { get; set; }

public int Id;

}
public class PeopleCopy
{
public int Age { get; set; }
public string Name { get; set; }

public int Id;
}

public class Class1
{
private static Dictionary<string, object> _Dic = new Dictionary<string, object>();
private static TOut TransExp<TIn, TOut>(TIn tIn) {
string key = $"funckey_{typeof(TIn).FullName}_{typeof(TOut).FullName}";
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
List<MemberBinding> memberBindingList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
memberBindingList.Add(Expression.Bind(item, property));
}
foreach (var item in typeof(TOut).GetFields())
{
MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
memberBindingList.Add(Expression.Bind(item, property));
}
Expression<Func<TIn, TOut>> expression = Expression.Lambda<Func<TIn, TOut>>(Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList), new ParameterExpression[]
{
parameterExpression
});
Func<TIn, TOut> func = expression.Compile();
_Dic.Add(key,func);
return func(tIn);
}

public static TOut TransExp2<TIn, TOut>(TIn tIn) {
if (_Dic.Keys.Contains($"funckey_{typeof(TIn).FullName}_{typeof(TOut).FullName}"))
{
Func<TIn, TOut> func = (Func<TIn, TOut>)_Dic[$"funckey_{typeof(TIn).FullName}_{typeof(TOut).FullName}"];
return func(tIn);
}
else
{
return TransExp<TIn, TOut>(tIn);
}
}}

static void Main(string[] args)
{

List<PeopleCopy> PeoleCopyList = new List<PeopleCopy>();
for (int i = 0; i < 5; i++)
{
People people = new People() { Id = 5+1, Age = 25, Name = "aaa"+i };
PeoleCopyList.Add(Class1.TransExp2<People, PeopleCopy>(people));
}

}

05-08 08:09