需求背景:对象复制性能优化;同时,在对象复制时,应跳过引用类型的null值复制,值类型支持值类型向可空类型的复制
using Common; using System; class Program { static void Main(string[] args) { TestClassA classA = new TestClassA() { PropA = new TestClass() { Name = "cs1" }, PropB = "c1", PropC = 1 }; TestClassA classB = new TestClassA() { PropA = new TestClass() { Name = "cs2" }, PropB = "c2", PropC = 2 }; FastCopy.Copy(classA, classB, false); Console.WriteLine(classB.PropA?.Name + ":" + classB.PropB + ":" + classB.PropC); TestClassA classC = new TestClassA() { PropA = new TestClass() { Name = "cs1" } }; TestClassA classD = new TestClassA() { PropA = new TestClass() { Name = "cs2" }, PropB = "c2", PropC = 2 }; FastCopy.Copy(classC, classD, false); Console.WriteLine(classD.PropA?.Name + ":" + classD.PropB + ":" + classD.PropC); } } public class TestClassA { public TestClass PropA { get; set; } public string PropB { get; set; } public int? PropC { get; set; } } public class TestClass { public string Name { get; set; } }
输出:
百万次调用耗时:270-300ms
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using static System.Linq.Expressions.Expression; namespace Common { public static class FastCopy { static ConcurrentDictionarycopiers = new ConcurrentDictionary (); /// /// 复制两个对象同名属性值 /// ////// /// 源对象 /// 目标对象 /// 源对象属性值为null时,是否将值复制给目标对象 public static void Copy (S source, T target, bool copyNull = true) { string name = string.Format("{0}_{1}_{2}", typeof(S), typeof(T), copyNull); object targetCopier; if (!copiers.TryGetValue(name, out targetCopier)) { Actioncopier = CreateCopier(copyNull); copiers.TryAdd(name, copier); targetCopier = copier; } Actionaction = (Action)targetCopier; action(source, target); } ////// 为指定的两种类型编译生成属性复制委托 /// ////// /// 源对象属性值为null时,是否将值复制给目标对象 /// private static Action CreateCopier(bool copyNull) { ParameterExpression source = Parameter(typeof(S)); ParameterExpression target = Parameter(typeof(T)); var sourceProps = typeof(S).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead).ToList(); var targetProps = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanWrite).ToList(); // 查找可进行赋值的属性 var copyProps = targetProps.Where(tProp => sourceProps.Where(sProp => sProp.Name == tProp.Name// 名称一致 且 && ( sProp.PropertyType == tProp.PropertyType// 属性类型一致 或 || sProp.PropertyType.IsAssignableFrom(tProp.PropertyType) // 源属性类型 为 目标属性类型 的 子类;eg:object target = string source; 或 || (tProp.PropertyType.IsValueType && sProp.PropertyType.IsValueType && // 属性为值类型且基础类型一致,但目标属性为可空类型 eg:int? num = int num; ((tProp.PropertyType.GenericTypeArguments.Length > 0 ? tProp.PropertyType.GenericTypeArguments[0] : tProp.PropertyType) == sProp.PropertyType)) )).Count() > 0); ListexpressionList = new List (); foreach (var prop in copyProps) { if (prop.PropertyType.IsValueType)// 属性为值类型 { PropertyInfo sProp = typeof(S).GetProperty(prop.Name); PropertyInfo tProp = typeof(T).GetProperty(prop.Name); if (sProp.PropertyType == tProp.PropertyType)// 属性类型一致 eg:int num = int num; 或 int? num = int? num; { var assign = Assign(Property(target, prop.Name), Property(source, prop.Name)); expressionList.Add(assign); } else if (sProp.PropertyType.GenericTypeArguments.Length <= 0 && tProp.PropertyType.GenericTypeArguments.Length > 0)// 属性类型不一致且目标属性类型为可空类型 eg:int? num = int num; { var convert = Convert(Expression.Property(source, prop.Name), tProp.PropertyType); var cvAssign = Assign(Expression.Property(target, prop.Name), convert); expressionList.Add(cvAssign); } } else// 属性为引用类型 { var assign = Assign(Property(target, prop.Name), Property(source, prop.Name));// 编译生成属性赋值语句 target.{PropertyName} = source.{PropertyName}; var sourcePropIsNull = Equal(Constant(null, prop.PropertyType), Property(source, prop.Name));// 判断源属性值是否为Null;编译生成 source.{PropertyName} == null var setNull = IsTrue(Constant(copyNull));// 判断是否复制Null值 编译生成 copyNull == True var setNullTest = IfThen(setNull, assign); var condition = IfThenElse(sourcePropIsNull, setNullTest, assign); /** * 编译生成 * if(source.{PropertyName} == null) * { * if(setNull) * { * target.{PropertyName} = source.{PropertyName}; * } * } * else * { * target.{PropertyName} = source.{PropertyName}; * } */ expressionList.Add(condition); } } var block = Block(expressionList.ToArray()); Expression > lambda = Lambda >(block, source, target); return lambda.Compile(); } } }
如果完整复制,去掉逻辑判断,同时可通过泛型类,不在使用字典,性能还可以提升。
using System; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace Common { public static class FastCopy{ static Actionaction = CreateCopier(); ////// 复制两个对象同名属性值 /// ////// /// 源对象 /// 目标对象 /// 源对象属性值为null时,是否将值复制给目标对象 public static void Copy(S source, T target, bool copyNull = true) { action(source, target); } /// /// 为指定的两种类型编译生成属性复制委托 /// ////// /// 源对象属性值为null时,是否将值复制给目标对象 /// private static Action CreateCopier() { ParameterExpression source = Expression.Parameter(typeof(S)); ParameterExpression target = Expression.Parameter(typeof(T)); var sourceProps = typeof(S).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead).ToList(); var targetProps = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanWrite).ToList(); // 查找可进行赋值的属性 var copyProps = targetProps.Where(tProp => sourceProps.Where(sProp => sProp.Name == tProp.Name// 名称一致 且 && ( sProp.PropertyType == tProp.PropertyType// 属性类型一致 )).Count() > 0); var block = Expression.Block(from p in copyProps select Expression.Assign(Expression.Property(target, p.Name), Expression.Property(source, p.Name))); Expression> lambda = Expression.Lambda >(block, source, target); return lambda.Compile(); } } }
百万次耗时:100ms左右
到此这篇关于C#使用表达式树实现对象复制的示例代码的文章就介绍到这了,更多相关C#表达式树对象复制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!