C#使用表達(dá)式樹實(shí)現(xiàn)對(duì)象復(fù)制的示例代碼
需求背景:對(duì)象復(fù)制性能優(yōu)化;同時(shí),在對(duì)象復(fù)制時(shí),應(yīng)跳過引用類型的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; }
}輸出:

百萬次調(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));// 編譯生成屬性賦值語句 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í)可通過泛型類,不在使用字典,性能還可以提升。
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();
}
}
}百萬次耗時(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#利用反射來判斷對(duì)象是否包含某個(gè)屬性的實(shí)現(xiàn)方法
這篇文章主要介紹了C#利用反射來判斷對(duì)象是否包含某個(gè)屬性的實(shí)現(xiàn)方法,很有借鑒價(jià)值的一個(gè)技巧,需要的朋友可以參考下2014-08-08
c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)
本篇文章主要介紹了c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01
C#獲取HTML文本的第一張圖片與截取內(nèi)容摘要示例代碼
在日常web開發(fā)的時(shí)候,經(jīng)常會(huì)遇到需要獲取保存的HTML文本中的第一張圖片,并且截取內(nèi)容摘要的效果,例如織夢(mèng)的后臺(tái)添加完詳細(xì)內(nèi)容后就是自動(dòng)讀取內(nèi)容摘要,并保存第一張圖片為縮略圖,那么這篇文章跟大家分享下利用C#如何實(shí)現(xiàn),感興趣的朋友們下面來一起看看吧。2016-10-10
C#探秘系列(四)——GetHashCode,ExpandoObject
這篇繼續(xù)分享下GetHashCode和ExpandoObject這兩個(gè)比較好玩的方法。2014-05-05
C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄
這篇文章主要為大家詳細(xì)介紹了C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04

