c# 表达式目录树拷贝对象(根据对象类型动态生成表达式目录树)
表达式目录树,在C#中用Expression标识,这里就不介绍表达式目录树是什么了,有兴趣可以自行百度搜索,网上资料还是很多的。
这里主要分享的是如何动态构建表达式目录树。
构建表达式目录树的代码挺简单的,但是感觉不容易记住,我这边主要是根据反编译工具ILSpy来查看自己写已经写好的一个表达式(反编译里面的代码就是拆解表达式后再拼装的,下面会给例子),然后再稍微改动一下,这样有助于理解如何去手动拼装一个表达式。
既然是拷贝对象,先来一个最简单的对象拷贝的表达式:
假设有类:
public class Cat { public string Name { get; set; } public string Colour { get; set; } } public class Dog { public string Name { get; set; } public string Colour { get; set; } }
有一天,我发现邻居的猫和我家的狗毛色长得一样,邻居的狗名字叫的很好听,颜色也很好看,于是,我也想给我家的猫取相同的名字,把毛色也染成相同的颜色:
Dog dog = new Dog { Name = "二哈", Colour = "黄色" }; Cat cat = new Cat { Name = dog.Name, Colour = dog.Colour};
上面两句代码,转换成表达式目录树:
//定义一个表达式 Expression<Func<Dog, Cat>> exp = d => new Cat { Name = d.Name, Colour = d.Colour };
把我家的猫也取相同名字,也染毛色:
Cat c = exp.Compile().Invoke(dog);
借助ILSpy反编译工具,查看表达式Expression<Func<Dog, Cat>> exp = d => new Cat { Name = d.Name, colour = d.Colour };反编译生产的代码
ParameterExpression parameterExpression = Expression.Parameter(typeof(Dog), "d"); Expression<Func<Dog, Cat>> expression = Expression.Lambda<Func<Dog, Cat>>(Expression.MemberInit(Expression.New(typeof(Cat)), new MemberBinding[] { Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(set_Name())), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(get_Name())))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(set_Colour())), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(get_Colour())))) }), new ParameterExpression[] { parameterExpression });
分析此代码,(MethodInfo)MethodBase.GetMethodFromHandle(ldtoken(set_Name())) 类似这种代码我们编译器是不认识的,替换成反射获取Set_Name方法,整理之后代码如下:
1 ParameterExpression parameterExpression = Expression.Parameter(typeof(Dog), "d"); //定义一个变量 2 3 Type typeDog = typeof(Dog); 4 MethodInfo getName = typeDog.GetMethod("get_Name"); //获取get_Name()方法 5 MethodInfo getColour = typeDog.GetMethod("get_Colour"); //获取get_Colour()方法 6 Type typeCat = typeof(Cat); 7 MethodInfo setName = typeCat.GetMethod("set_Name"); //获取set_Name()方法 8 MethodInfo setColour = typeCat.GetMethod("set_Colour"); //获取set_Colour()方法 9 10 11 MemberExpression dName = Expression.Property(parameterExpression, getName);//d.Name 12 MemberAssignment name_dName = Expression.Bind(setName, dName);//Name = d.Name 13 MemberExpression dColour = Expression.Property(parameterExpression, getColour);//d.Colour 14 MemberAssignment colour_dColour = Expression.Bind(setColour, dColour);//Colour = d.Colour 15 16 //new Cat { Name = d.Name, Colour = d.Colour }; 表达式主体完成 17 MemberInitExpression body = Expression.MemberInit(Expression.New(typeof(Cat)), 18 new MemberBinding[] 19 { 20 name_dName, 21 colour_dColour 22 }); 23 //转换为表达式 24 Expression<Func<Dog, Cat>> expression = Expression.Lambda<Func<Dog, Cat>>(body 25 , new ParameterExpression[] 26 { 27 parameterExpression 28 } 29 ); 30 31 Cat c2 = expression.Compile().Invoke(dog);//编译表达式树由描述为可执行代码的 lambda 表达式,并生成一个委托执行
到这里,很多人会有疑问,你都自己写了表达式目录树了,还手动拼装一个一模一样的?而且手动拼装这么麻烦?有什么作用?往下看例子
有天去动物园,看见一只老虎,于是我又想到邻居的狗和我家的猫...于是乎我又想把自家的猫当做老虎了...于是,又写了下面这条表达式
Expression<Func<Tiger, Cat>> exp = d => new Cat { Name = d.Name, Colour = d.Colour };
但是这样子不对啊?难道我每次换个类型,都要写一条表达式吗?这样明显是不合适的,所以我们需要封装一个方法,动态拼装出两个对象属性赋值的完整表达式,这个封装就不详细写了,上面已经写了如何去拼装一个表达式了,虽然只有部分,更多的可以自己写个复杂的表达式查看反编译代码。
这里用到泛型类作为缓存,否则此种写法没意义,还不如直接用反射来写。
单层拷贝:
/// <summary> /// 动态生成表达式目录树方式 浅拷贝 对象 /// 采用泛型类作为静态缓存,即<TIn,TOut>同一类型只会生成一次表达式目录树。(如果不缓存,和直接存反射没啥区别,不如直接用纯反射的方式深拷贝) /// </summary> /// <typeparam name="TIn"></typeparam> /// <typeparam name="TOut"></typeparam> public class ExpressionCloneObject<TIn,TOut> { private static Func<TIn, TOut> _FUNC = null; static ExpressionCloneObject() { _FUNC = _CloneFunc(); } public static TOut Invoke(TIn t) { return _FUNC.Invoke(t); } private static Func<TIn, TOut> _CloneFunc() { Type typeT1 = typeof(TIn); Type typeT2 = typeof(TOut); ParameterExpression parameterExpression = Expression.Parameter(typeT1, "t"); List<MemberBinding> memberList = new List<MemberBinding>(); //属性 foreach (PropertyInfo propertie in typeT2.GetProperties()) { PropertyInfo propertieT1 = typeT1.GetProperty(propertie.Name); if (propertieT1 != null) { MethodInfo mSet = typeT2.GetMethod("set_" + propertie.Name, new Type[] { propertie.PropertyType }); memberList.Add(Expression.Bind(mSet, Expression.Property(parameterExpression, propertieT1))); } else { FieldInfo fieldT1 = typeT1.GetField(propertie.Name);//可能T2中的属性对应T1中的字段 if (fieldT1 != null) { MethodInfo mSet = typeT2.GetMethod("set_" + propertie.Name, new Type[] { propertie.PropertyType }); memberList.Add(Expression.Bind(mSet, Expression.Field(parameterExpression, fieldT1))); } } } //字段 foreach (FieldInfo field in typeT2.GetFields()) { FieldInfo fieldT1 = typeT1.GetField(field.Name); if (fieldT1 != null) { memberList.Add(Expression.Bind(field, Expression.Field(parameterExpression, fieldT1))); } else { PropertyInfo propertieT1 = typeT1.GetProperty(field.Name); if (propertieT1 != null)//可能T2中的字段对应T1中的属性 { memberList.Add(Expression.Bind(field, Expression.Property(parameterExpression, propertieT1))); } } } MemberInitExpression body = Expression.MemberInit( Expression.New(typeT2) , memberList.ToArray() ); Expression<Func<TIn, TOut>> expression = Expression.Lambda<Func<TIn, TOut>>(body, new ParameterExpression[] { parameterExpression }); return expression.Compile(); } }
深层拷贝:
/// <summary> /// /// 动态生成表达式目录树方式 深拷贝 对象 /// 采用泛型类作为静态缓存,即<TIn,TOut>同一类型只会生成一次表达式目录树。(如果不缓存,和直接存反射没啥区别,不如直接用纯反射的方式拷贝) /// (注:对象的属性 不支持数组或集合,原因是对象中的数组或集合的个数可能不一样 /// ,并且这里用泛型缓存,所以以第一次的数组或集合生成的表达式目录树会缓存起来,下次直接拷贝对象如果集合中的个数和缓存中的不一致 /// 会导致拷贝结果不正确。解决方法可以先拷贝属性中的集合,再赋值回到此方法拷贝出来的对象上。或者换种深拷贝方法,深拷贝方式很多,如 /// 序列化、存反射、AutoMapper等等) /// </summary> /// <typeparam name="TIn"></typeparam> /// <typeparam name="TOut"></typeparam> public class ExpressionDeepCloneObject<TIn,TOut> where TOut : class, new() { private static Func<TIn, TOut> _FUNC = null; public static TOut Invoke(TIn t) { if(_FUNC == null) { _FUNC = _DeepClone(t); } return _FUNC.Invoke(t); } /// <summary> /// 表达式目录树深拷贝 /// </summary> /// <typeparam name="TIn">被拷贝对象类型</typeparam> /// <typeparam name="TOut">新对象类型</typeparam> /// <param name="tIn">被拷贝对象</param> /// <returns>表达式目录树Lambda</returns> private static Func<TIn, TOut> _DeepClone(TIn tIn) { TOut tOut = new TOut(); ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "t"); Expression<Func<TIn, TOut>> expression = Expression.Lambda<Func<TIn, TOut>>(_DeepClone(tIn, tOut, parameterExpression), new ParameterExpression[] { parameterExpression }); return expression.Compile(); } private static MemberInitExpression _DeepClone(object t1, object t2, Expression parameterExpression) { Type typeT1 = t1.GetType(); Type typeT2 = t2.GetType(); List<MemberBinding> memberList = new List<MemberBinding>(); //属性部分 foreach (PropertyInfo propertie in typeT2.GetProperties()) { PropertyInfo propertieT1 = typeT1.GetProperty(propertie.Name); if (propertieT1 != null && propertieT1.GetValue(t1) != null) { MethodInfo mSet = typeT2.GetMethod("set_" + propertie.Name, new Type[] { propertie.PropertyType }); if (!propertieT1.GetValue(t1).GetType().IsValueType && propertieT1.GetValue(t1).GetType().Name != "String")//引用类型(不包括string) { memberList.Add(Expression.Bind(mSet, _DeepClone(propertieT1.GetValue(t1), propertieT1.GetValue(t1), Expression.Property(parameterExpression, propertieT1)))); } else { memberList.Add(Expression.Bind(mSet, Expression.Property(parameterExpression, propertieT1))); } } else { FieldInfo fieldT1 = typeT1.GetField(propertie.Name);//可能t2中的属性对应t1中的字段 if (fieldT1 != null && fieldT1.GetValue(t1) != null) { MethodInfo mSet = typeT2.GetMethod("set_" + propertie.Name, new Type[] { propertie.PropertyType }); if (!fieldT1.GetValue(t1).GetType().IsValueType && fieldT1.GetValue(t1).GetType().Name != "String")//引用类型(不包括string) { memberList.Add(Expression.Bind(mSet, _DeepClone(fieldT1.GetValue(t1), fieldT1.GetValue(t1), Expression.Field(parameterExpression, fieldT1)))); } else { memberList.Add(Expression.Bind(mSet, Expression.Field(parameterExpression, fieldT1))); } } } } //字段部分 foreach (FieldInfo field in typeT2.GetFields()) { FieldInfo fieldT1 = typeT1.GetField(field.Name); if (fieldT1 != null && fieldT1.GetValue(t1) != null) { if (!fieldT1.GetValue(t1).GetType().IsValueType && fieldT1.GetValue(t1).GetType().Name != "String")//引用类型(不包括string) { memberList.Add(Expression.Bind(field, _DeepClone(fieldT1.GetValue(t1), fieldT1.GetValue(t1), Expression.Field(parameterExpression, fieldT1)))); } else { memberList.Add(Expression.Bind(field, Expression.Field(parameterExpression, fieldT1))); } } else { PropertyInfo propertieT1 = typeT1.GetProperty(field.Name); if (propertieT1 != null && propertieT1.GetValue(t1) != null)//可能t2中的字段对应t1中的属性 { if (!propertieT1.GetValue(t1).GetType().IsValueType && propertieT1.GetValue(t1).GetType().Name != "String") //引用类型(不包括string) { memberList.Add(Expression.Bind(field, _DeepClone(propertieT1.GetValue(t1), propertieT1.GetValue(t1), Expression.Property(parameterExpression, propertieT1)))); } else { memberList.Add(Expression.Bind(field, Expression.Property(parameterExpression, propertieT1))); } } } } MemberInitExpression body = Expression.MemberInit( Expression.New(typeT2) , memberList.ToArray() ); return body; } }
当有这个封装之后,只需要如下写,会自动拼装出想要的表达式返回想要的结果
Cat c3 = ExpressionCloneObject<Tiger, Cat>.Invoke(new Tiger { Name = "Tiger", Colour = "黄色" });
转 https://www.cnblogs.com/fnz0/p/11468723.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2019-01-21 前后端分手大师——MVVM 模式