C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#表达式树对象复制

C#使用表达式树实现对象复制的示例代码

作者:jack_Meng

这篇文章主要介绍了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#使用表达式树实现对象复制的示例代码的文章就介绍到这了,更多相关C#表达式树对象复制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文