广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)
  • 329
分享到

C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)

2024-04-02 19:04:59 329人浏览 泡泡鱼
摘要

有看过我之前发表过的C#相关文章分享和阅读过我代码的朋友们可能会在我的代码里面经常看到各种各样的λ表达式动态拼接,C#的λ表达式树是一个好东西,也是别的语

有看过我之前发表过的C#相关文章分享和阅读过我代码的朋友们可能会在我的代码里面经常看到各种各样的λ表达式动态拼接,C#的λ表达式树是一个好东西,也是别的语言学不来的,熟悉掌握λ表达式就能够实现各种场景的个性化操作,如动态拼接查询条件、排序方式等,也能够实现替代反射的高性能操作,比如我们常用到的IQueryable和IEnumerable,每个扩展方法就全是λ表达式树。

本文给大家分享C#使用表达式树(LambdaExpression)动态更新类的属性值的相关知识,在某些业务中会遇到需要同步两个类的属性值的情况,而且有些字段是要过滤掉的。如果手动赋值则需要写很多重复的代码:

 public class Teacher
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
        }
        public class Student
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
        }
   /// <summary>
        /// 比如需要把teacher的某些属性值赋给student,而id不需要赋值
        /// </summary>
        /// <param name="student"></param>
        /// <param name="teacher"></param>
        public static void SetProperty(Student student, Teacher teacher)
        {
            if (student.Name != teacher.Name)
            {
                student.Name = teacher.Name;
            }
            if (student.Age != teacher.Age)
            {
                student.Age = teacher.Age;
            }
        }

使用反射的话性能考虑,尝试写一个扩展方法使用lambda表达式树去构建一个方法


public static class ObjectExtensions
    {
        /// <summary>
        /// 缓存表达式
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TTarget"></typeparam>
        public static class MapperAccessor<TSource, TTarget>
        {
            private static Action<TSource, TTarget, string[]> func { get; set; }
            public static TSource Set(TSource source, TTarget target, params string[] properties)
            {
                if (func == null)
                {
                    var sourceType = typeof(TSource);
                    var targetType = typeof(TTarget);
                    BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
                    if (properties.Length == 0)
                    {
                        //get all properties
                        if (sourceType == targetType)
                        {
                            //如果是相同类型则获取所有属性
                            properties = sourceType.GetProperties(bindingFlags).Select(x => x.Name)
                                .ToArray();
                        }
                        else
                        {
                            //如果没有传指定的属性则默认获取同名属性
                            List<PropertyInfo> propertyInfos = new List<PropertyInfo>();
                            foreach (var property in sourceType.GetProperties(bindingFlags))
                            {//不同类型指定同名且类型相同的属性
                                var targetProperty = targetType.GetProperty(property.Name, bindingFlags);
                                if (targetProperty != null && targetProperty.PropertyType == property.PropertyType)
                                {
                                    propertyInfos.Add(property);
                                }
                            }
                            properties = propertyInfos.Select(x => x.Name).ToArray();
                        }
                    }
                    //定义lambda 3个参数
                    var s = Expression.Parameter(typeof(TSource), "s");
                    var t = Expression.Parameter(typeof(TTarget), "t");
                    var ps = Expression.Parameter(typeof(string[]), "ps");
                    //获取泛型扩展方法Contains
                    var methodInfo = typeof(Enumerable).GetMethods().FirstOrDefault(e => e.Name == "Contains" && e.GetParameters().Length == 2);
                    if (methodInfo == null)
                    {
                        // properties.Contains()
                        throw new NullReferenceException(nameof(methodInfo));
                    }
                    MethodInfo genericMethod = methodInfo.MakeGenericMethod(typeof(String));//创建泛型方法
                    List<BlockExpression> bs = new List<BlockExpression>();
                    foreach (string field in properties)
                    {
                        //获取两个类型里面的属性
                        var sourceField = Expression.Property(s, field);
                        var targetField = Expression.Property(t, field);
                        //创建一个条件表达式
                        var notEqual = Expression.NotEqual(sourceField, targetField);//sourceField!=targetField
                        var method = Expression.Call(null, genericMethod, ps, Expression.Constant(field));//ps.Contains(f);
                        //构建赋值语句
                        var ifTrue = Expression.Assign(sourceField, targetField);
                        //拼接表达式 sourceField!=targetField&&ps.Contains(f)
                        var condition = Expression.And(notEqual, Expression.IsTrue(method));
                        //判断是否相同,如果不相同则赋值
                        var expression = Expression.IfThen(condition, ifTrue);
                        bs.Add(Expression.Block(expression));
                    }
                    var lambda = Expression.Lambda<Action<TSource, TTarget, string[]>>(Expression.Block(bs), s, t, ps);
                    func = lambda.Compile();
                }
                func.Invoke(source, target, properties);
                return source;
            }
        }
        /// <summary>
        /// 通过目标类更新源类同名属性值
        /// </summary>
        /// <typeparam name="TSource">待更新的数据类型</typeparam>
        /// <typeparam name="TTarget">目标数据类型</typeparam>
        /// <param name="source">源数据</param>
        /// <param name="target">目标数据</param>
        /// <param name="properties">要变更的属性名称</param>
        /// <returns>返回源数据,更新后的</returns>
        public static TSource SetProperties<TSource, TTarget>(this TSource source, TTarget target, params string[] properties)
        {
            return MapperAccessor<TSource, TTarget>.Set(source, target, properties);
        }
    }

编写测试方法


 /// <summary>
        /// 比如需要把teacher的某些属性值赋给student,而id不需要赋值
        /// </summary>
        /// <param name="student"></param>
        /// <param name="teacher"></param>
        public static void SetProperty(Student student, Teacher teacher)
        {
            if (student.Name != teacher.Name)
            {
                student.Name = teacher.Name;
            }
            if (student.Age != teacher.Age)
            {
                student.Age = teacher.Age;
            }
        }
        public static void SetProperty2(Student student, Teacher teacher, params string[] properties)
        {
            var sourceType = student.GetType();
            var targetType = teacher.GetType();
            foreach (var property in properties)
            {
                var aP = sourceType.GetProperty(property);
                var bP = targetType.GetProperty(property);
                var apValue = aP.GetValue(student);
                var bpValue = bP.GetValue(teacher);
                if (apValue != bpValue)
                {
                    aP.SetValue(student, bpValue);
                }
            }
        }

        static (List<Student>, List<Teacher>) CreateData(int length)
        {
            var rd = new Random();
            (List<Student>, List<Teacher>) ret;
            ret.Item1 = new List<Student>();
            ret.Item2 = new List<Teacher>();
            for (int i = 0; i < length; i++)
            {
                Student student = new Student()
                {
                    Id = Guid.NewGuid(),
                    Name = Guid.NewGuid().ToString("N"),
                    Age = rd.Next(1, 100)
                };
                ret.Item1.Add(student);
                Teacher teacher = new Teacher()
                {
                    Id = Guid.NewGuid(),
                    Name = Guid.NewGuid().ToString("N"),
                    Age = rd.Next(1, 100)
                };
                ret.Item2.Add(teacher);
            }
            return ret;
        }
        static void Main(string[] args)
        {

            var length = 1000000;
            var data = CreateData(length);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < length; i++)
            {
                SetProperty(data.Item1[i], data.Item2[i]);
            }
            sw.Stop();
            Console.WriteLine($"手写方法耗时:{sw.ElapsedMilliseconds}ms");
            data.Item1.Clear();
            data.Item2.Clear();
            var data2 = CreateData(length);
            sw.Restart();
            for (int i = 0; i < length; i++)
            {
                data2.Item1[i].SetProperties(data2.Item2[i], nameof(Student.Age), nameof(Student.Name));
            }
            data2.Item1.Clear();
            data2.Item2.Clear();
            sw.Stop();
            Console.WriteLine($"lambda耗时:{sw.ElapsedMilliseconds}ms");
            var data3 = CreateData(length);
            sw.Restart();
            for (int i = 0; i < length; i++)
            {
                SetProperty2(data3.Item1[i], data3.Item2[i], nameof(Student.Age), nameof(Student.Name));
            }
            sw.Stop();
            Console.WriteLine($"反射耗时:{sw.ElapsedMilliseconds}ms");
            data3.Item1.Clear();
            data3.Item2.Clear();
      
            Console.ReadKey();
        }

可以看到性能和手写方法之间的差距,如果要求比较高还是手写方法,如果字段多的话写起来是很痛苦的事。但是日常用这个足够了,而且是扩展方法,通用性很强。

到此这篇关于C#使用表达式树(LambdaExpression)动态更新类的属性值的文章就介绍到这了,更多相关C#表达式树类的属性值内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)

本文链接: https://www.lsjlt.com/news/162175.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)
    有看过我之前发表过的C#相关文章分享和阅读过我代码的朋友们可能会在我的代码里面经常看到各种各样的λ表达式动态拼接,C#的λ表达式树是一个好东西,也是别的语...
    99+
    2022-11-12
  • C#如何使用表达式树动态更新类的属性值
    本篇内容介绍了“C#如何使用表达式树动态更新类的属性值”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!C#的&lambda;表达式树是...
    99+
    2023-06-26
  • C#使用表达式树实现对象复制的示例代码
    需求背景:对象复制性能优化;同时,在对象复制时,应跳过引用类型的null值复制,值类型支持值类型向可空类型的复制 using Common; using System; class ...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作