您好,登錄后才能下訂單哦!
本篇內容主要講解“C#使用表達式樹怎么實現對象復制”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C#使用表達式樹怎么實現對象復制”吧!
需求背景:對象復制性能優化;同時,在對象復制時,應跳過引用類型的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 ConcurrentDictionary<string, object> copiers = new ConcurrentDictionary<string, object>(); /// <summary> /// 復制兩個對象同名屬性值 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="source">源對象</param> /// <param name="target">目標對象</param> /// <param name="copyNull">源對象屬性值為null時,是否將值復制給目標對象</param> public static void Copy<S, T>(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)) { Action<S, T> copier = CreateCopier<S, T>(copyNull); copiers.TryAdd(name, copier); targetCopier = copier; } Action<S, T> action = (Action<S, T>)targetCopier; action(source, target); } /// <summary> /// 為指定的兩種類型編譯生成屬性復制委托 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="copyNull">源對象屬性值為null時,是否將值復制給目標對象</param> /// <returns></returns> private static Action<S, T> CreateCopier<S, T>(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); List<Expression> expressionList = new List<Expression>(); 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<Action<S, T>> lambda = Lambda<Action<S, T>>(block, source, target); return lambda.Compile(); } } }
如果完整復制,去掉邏輯判斷,同時可通過泛型類,不在使用字典,性能還可以提升。
using System; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace Common { public static class FastCopy<S, T> { static Action<S, T> action = CreateCopier(); /// <summary> /// 復制兩個對象同名屬性值 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="source">源對象</param> /// <param name="target">目標對象</param> /// <param name="copyNull">源對象屬性值為null時,是否將值復制給目標對象</param> public static void Copy(S source, T target, bool copyNull = true) { action(source, target); } /// <summary> /// 為指定的兩種類型編譯生成屬性復制委托 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="copyNull">源對象屬性值為null時,是否將值復制給目標對象</param> /// <returns></returns> private static Action<S, T> 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<Action<S, T>> lambda = Expression.Lambda<Action<S, T>>(block, source, target); return lambda.Compile(); } } }
百萬次耗時:100ms左右
到此,相信大家對“C#使用表達式樹怎么實現對象復制”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。