C#使用表達(dá)式樹實(shí)現(xiàn)對(duì)象復(fù)制的示例代碼
需求背景:對(duì)象復(fù)制性能優(yōu)化;同時(shí),在對(duì)象復(fù)制時(shí),應(yīng)跳過(guò)引用類型的null值復(fù)制,值類型支持值類型向可空類型的復(fù)制
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; } }
輸出:
百萬(wàn)次調(diào)用耗時(shí):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> /// 復(fù)制兩個(gè)對(duì)象同名屬性值 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="source">源對(duì)象</param> /// <param name="target">目標(biāo)對(duì)象</param> /// <param name="copyNull">源對(duì)象屬性值為null時(shí),是否將值復(fù)制給目標(biāo)對(duì)象</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> /// 為指定的兩種類型編譯生成屬性復(fù)制委托 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="copyNull">源對(duì)象屬性值為null時(shí),是否將值復(fù)制給目標(biāo)對(duì)象</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(); // 查找可進(jìn)行賦值的屬性 var copyProps = targetProps.Where(tProp => sourceProps.Where(sProp => sProp.Name == tProp.Name// 名稱一致 且 && ( sProp.PropertyType == tProp.PropertyType// 屬性類型一致 或 || sProp.PropertyType.IsAssignableFrom(tProp.PropertyType) // 源屬性類型 為 目標(biāo)屬性類型 的 子類;eg:object target = string source; 或 || (tProp.PropertyType.IsValueType && sProp.PropertyType.IsValueType && // 屬性為值類型且基礎(chǔ)類型一致,但目標(biāo)屬性為可空類型 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)// 屬性類型不一致且目標(biāo)屬性類型為可空類型 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));// 編譯生成屬性賦值語(yǔ)句 target.{PropertyName} = source.{PropertyName}; var sourcePropIsNull = Equal(Constant(null, prop.PropertyType), Property(source, prop.Name));// 判斷源屬性值是否為Null;編譯生成 source.{PropertyName} == null var setNull = IsTrue(Constant(copyNull));// 判斷是否復(fù)制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(); } } }
如果完整復(fù)制,去掉邏輯判斷,同時(shí)可通過(guò)泛型類,不在使用字典,性能還可以提升。
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> /// 復(fù)制兩個(gè)對(duì)象同名屬性值 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="source">源對(duì)象</param> /// <param name="target">目標(biāo)對(duì)象</param> /// <param name="copyNull">源對(duì)象屬性值為null時(shí),是否將值復(fù)制給目標(biāo)對(duì)象</param> public static void Copy(S source, T target, bool copyNull = true) { action(source, target); } /// <summary> /// 為指定的兩種類型編譯生成屬性復(fù)制委托 /// </summary> /// <typeparam name="S"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="copyNull">源對(duì)象屬性值為null時(shí),是否將值復(fù)制給目標(biāo)對(duì)象</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(); // 查找可進(jìn)行賦值的屬性 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(); } } }
百萬(wàn)次耗時(shí):100ms左右
到此這篇關(guān)于C#使用表達(dá)式樹實(shí)現(xiàn)對(duì)象復(fù)制的示例代碼的文章就介紹到這了,更多相關(guān)C#表達(dá)式樹對(duì)象復(fù)制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#使用Task實(shí)現(xiàn)執(zhí)行并行任務(wù)的原理的示例詳解
Task是一個(gè)表示異步操作的類,它提供了一種簡(jiǎn)單、輕量級(jí)的方式來(lái)創(chuàng)建多線程應(yīng)用程序。本文就來(lái)和大家聊聊在C#中如何使用Task執(zhí)行并行任務(wù)吧2023-04-04C#實(shí)現(xiàn)的Win32控制臺(tái)線程計(jì)時(shí)器功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)的Win32控制臺(tái)線程計(jì)時(shí)器功能,結(jié)合實(shí)例形式分析了C#基于控制臺(tái)的時(shí)間操作相關(guān)技巧,需要的朋友可以參考下2016-08-08C# OpenCvSharp實(shí)現(xiàn)去除文字中的線條
這篇文章主要為大家詳細(xì)介紹了C#如何使用OpenCvSharp實(shí)現(xiàn)去除文字中的線條效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11C#中使用IrisSkin2.dll美化WinForm程序界面的方法
這篇文章主要介紹了c#中使用IrisSkin2.dll美化WinForm程序界面的實(shí)現(xiàn)方法,需要的朋友可以參考下2013-05-05