利用lambda表达式树优化反射详解

 更新时间:2020年6月25日 11:15  点击:1725

前言

本节重点不讲反射机制,而是讲lambda表达式树来替代反射中常用的获取属性和方法,来达到相同的效果但却比反射高效。

每个人都知道,用反射调用一个方法或者对属性执行SetValue和GetValue操作的时候都会比直接调用慢很多,这其中设计到CLR中内部的处理,不做深究。然而,我们在某些情况下又无法不使用反射,比如:在一个ORM框架中,你要将一个DataRow转化为一个对象,但你又不清楚该对象有什么属性,这时候你就需要写一个通用的泛型方法来处理,以下代码写得有点恶心,但不妨碍理解意思:

//将DataReader转化为一个对象
     private static T GetObj<T>(SqliteDataReader reader) where T : class
 {
  T obj = new T();
  PropertyInfo[] pros = obj.GetType().GetProperties();
  foreach (PropertyInfo item in pros)
  {
  try
  {
   Int32 Index = reader.GetOrdinal(item.Name);
   String result = reader.GetString(Index);
   if (typeof(String) == item.PropertyType)
   {
   item.SetValue(obj, result);
   continue;
   }
   if (typeof(DateTime) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToDateTime(result));
   continue;
   }
   if (typeof(Boolean) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToBoolean(result));
   continue;
   }
   if (typeof(Int32) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToInt32(result));
   continue;
   }
   if (typeof(Single) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToSingle(result));
   continue;
   }
   if (typeof(Single) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToSingle(result));
   continue;
   }
   if (typeof(Double) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToDouble(result));
   continue;
   }
   if (typeof(Decimal) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToDecimal(result));
   continue;
   }
   if (typeof(Byte) == item.PropertyType)
   {
   item.SetValue(obj, Convert.ToByte(result));
   continue;
   }
  }
  catch (ArgumentOutOfRangeException ex)
  {
   continue;
  }
  }
  return obj;
 }

对于这种情况,其执行效率是特别低下的,具体多慢在下面例子会在.Net Core平台上和.Net Framework4.0运行测试案例.对于以上我举例的情况,效率上我们还可以得到提升。但对于想在运行时修改一下属性的名称或其他操作,反射还是一项特别的神器,因此在某些情况下反射还是无法避免的。

但是对于只是简单的SetValue或者GetValue,包括用反射构造函数,我们可以想一个中继的方法,那就是使用表达式树。对于不理解表达式树的,可以到微软文档查看,点击我。表达式树很容易通过对象模型表示表达式,因此强烈建议学习。查看以下代码:

static void Main()
 {
  Dog dog = new Dog();
  PropertyInfo propertyInfo = dog.GetType().GetProperty(nameof(dog.Name)); //获取对象Dog的属性
  MethodInfo SetterMethodInfo = propertyInfo.GetSetMethod(); //获取属性Name的set方法

  ParameterExpression param = Expression.Parameter(typeof(Dog), "param");
  Expression GetPropertyValueExp = Expression.Lambda(Expression.Property(param, nameof(dog.Name)), param);
  Expression<Func<Dog, String>> GetPropertyValueLambda = (Expression<Func<Dog, String>>)GetPropertyValueExp;
  ParameterExpression paramo = Expression.Parameter(typeof(Dog), "param");
  ParameterExpression parami = Expression.Parameter(typeof(String), "newvalue");
  MethodCallExpression MethodCallSetterOfProperty = Expression.Call(paramo, SetterMethodInfo, parami);
  Expression SetPropertyValueExp = Expression.Lambda(MethodCallSetterOfProperty, paramo, parami);
  Expression<Action<Dog, String>> SetPropertyValueLambda = (Expression<Action<Dog, String>>)SetPropertyValueExp;

  //创建了属性Name的Get方法表达式和Set方法表达式,当然只是最简单的
  Func<Dog, String> Getter = GetPropertyValueLambda.Compile(); 
  Action<Dog, String> Setter = SetPropertyValueLambda.Compile();

  Setter?.Invoke(dog, "WLJ"); //我们现在对dog这个对象的Name属性赋值
  String dogName = Getter?.Invoke(dog); //获取属性Name的值
  
  Console.WriteLine(dogName);
  Console.ReadKey();
 }

 public class Dog
 {
  public String Name { get; set; }
 }

以下代码可能很难看得懂,但只要知道我们创建了属性的Get、Set这两个方法就行,其结果最后也能输出狗的名字 WLJ,拥有ExpressionTree的好处是他有一个名为Compile()的方法,它创建一个代表表达式的代码块。现在是最有趣的部分,假设你在编译时不知道类型(在这篇文章中包含的代码我在不同的程序集上创建了一个类型)你仍然可以应用这种技术,我将对于常用的属性的set,get操作进行分装。

/// <summary>
   /// 属性类,仿造反射中的PropertyInfo
 /// </summary>
   public class Property
 {

  private readonly PropertyGetter getter;
  private readonly PropertySetter setter;
  public String Name { get; private set; }

  public PropertyInfo Info { get; private set; }

  public Property(PropertyInfo propertyInfo)
  {
   if (propertyInfo == null)
    throw new NullReferenceException("属性不能为空");
   this.Name = propertyInfo.Name;
   this.Info = propertyInfo;
   if (this.Info.CanRead)
   {
    this.getter = new PropertyGetter(propertyInfo);
   }

   if (this.Info.CanWrite)
   {
    this.setter = new PropertySetter(propertyInfo);
   }
  }


  /// <summary>
     /// 获取对象的值
  /// </summary>
    /// <param name="instance"></param>
    /// <returns></returns>
     public Object GetValue(Object instance)
  {
   return getter?.Invoke(instance);
  }


  /// <summary>
     /// 赋值操作
  /// </summary>
    /// <param name="instance"></param>
    /// <param name="value"></param>
     public void SetValue(Object instance, Object value)
  {
   this.setter?.Invoke(instance, value);
  }

  private static readonly ConcurrentDictionary<Type, Core.Reflection.Property[]> securityCache = new ConcurrentDictionary<Type, Property[]>();

  public static Core.Reflection.Property[] GetProperties(Type type)
  {
   return securityCache.GetOrAdd(type, t => t.GetProperties().Select(p => new Property(p)).ToArray());
  }

 }

  /// <summary>
   /// 属性Get操作类
  /// </summary>
    public class PropertyGetter
  {
  private readonly Func<Object, Object> funcGet;

  public PropertyGetter(PropertyInfo propertyInfo) : this(propertyInfo?.DeclaringType, propertyInfo.Name)
  {

  }

  public PropertyGetter(Type declareType, String propertyName)
  {
   if (declareType == null)
   {
    throw new ArgumentNullException(nameof(declareType));
   }
   if (propertyName == null)
   {
    throw new ArgumentNullException(nameof(propertyName));
   }



   this.funcGet = CreateGetValueDeleagte(declareType, propertyName);
  }


  //代码核心部分
     private static Func<Object, Object> CreateGetValueDeleagte(Type declareType, String propertyName)
  {
   // (object instance) => (object)((declaringType)instance).propertyName

       var param_instance = Expression.Parameter(typeof(Object));
   var body_objToType = Expression.Convert(param_instance, declareType);
   var body_getTypeProperty = Expression.Property(body_objToType, propertyName);
   var body_return = Expression.Convert(body_getTypeProperty, typeof(Object));
   return Expression.Lambda<Func<Object, Object>>(body_return, param_instance).Compile();
  }

  public Object Invoke(Object instance)
  {
   return this.funcGet?.Invoke(instance);
  }
 }


  public class PropertySetter
 {
  private readonly Action<Object, Object> setFunc;

  public PropertySetter(PropertyInfo property) 
  {
   if (property == null)

   {
    throw new ArgumentNullException(nameof(property));
   }
   this.setFunc = CreateSetValueDelagate(property);
  }



  private static Action<Object, Object> CreateSetValueDelagate(PropertyInfo property)
  {
   // (object instance, object value) => 
   //  ((instanceType)instance).Set_XXX((propertyType)value)

   //声明方法需要的参数
   var param_instance = Expression.Parameter(typeof(Object));
   var param_value = Expression.Parameter(typeof(Object));

   var body_instance = Expression.Convert(param_instance, property.DeclaringType);
   var body_value = Expression.Convert(param_value, property.PropertyType);
   var body_call = Expression.Call(body_instance, property.GetSetMethod(), body_value);

   return Expression.Lambda<Action<Object, Object>>(body_call, param_instance, param_value).Compile();
  }

  public void Invoke(Object instance, Object value)
  {
   this.setFunc?.Invoke(instance, value);
  }
 }

在将代码应用到实例:

   Dog dog = new Dog();
   PropertyInfo propertyInfo = dog.GetType().GetProperty(nameof(dog.Name));
   
   //反射操作
   propertyInfo.SetValue(dog, "WLJ");
   String result = propertyInfo.GetValue(dog) as String;
   Console.WriteLine(result);
   
   //表达式树的操作
   Property property = new Property(propertyInfo);
   property.SetValue(dog, "WLJ2");
   String result2 = propertyInfo.GetValue(dog) as String;
   Console.WriteLine(result2);

发现其实现的目的与反射一致,但效率却有明显的提高。

以下测试以下他们两之间的效率。测试代码如下:

   Student student = new Student();
   PropertyInfo propertyInfo = student.GetType().GetProperty(nameof(student.Name));
   Property ExpProperty = new Property(propertyInfo);

   Int32 loopCount = 1000000;
   CodeTimer.Initialize(); //测试环境初始化

   //下面该方法个执行1000000次

   CodeTimer.Time("基础反射", loopCount, () => { 
    propertyInfo.SetValue(student, "Fode",null);
   });
   CodeTimer.Time("lambda表达式树", loopCount, () => {
    ExpProperty.SetValue(student, "Fode");
   });
   CodeTimer.Time("直接赋值", loopCount, () => {
    student.Name = "Fode";
   });
   Console.ReadKey();

其.Net4.0环境下运行结果如下:

.Net Core环境下运行结果:

从以上结果可以知道,迭代同样的次数反射需要183ms,而用表达式只要34ms,直接赋值需要7ms,在效率上,使用表达式这种方法有显著的提高,您可以看到使用此技术可以完全避免使用反射时的性能损失。反射之所以效率有点低主要取决于其加载的时候时在运行期下,而表达式则在编译期,下篇有空将会介绍用Emit技术优化反射,会比表达式略快一点。

注:对于常用对象的属性,最好将其缓存起来,这样效率会更高。。

代码下载

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对猪先飞的支持。

[!--infotagslink--]

相关文章

  • C# Lambda 知识回顾

    本文主要介绍了C#中Lambda的相关知识。具有一定的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 关于C#基础知识回顾--反射(一)

    其实说白了,反射就是能知道我们未知类型的类型信息这么一个东西.没什么神秘可讲!反射的核心是System.Type。System.Type包含了很多属性和方法,使用这些属性和方法可以在运行时得到类型信息...2020-06-25
  • 关于C#反射 你需要知道的

    这篇文章主要介绍了C#反射的相关知识,文中讲解的非常详细,代码帮助大家更好的参考学习,感兴趣的朋友可以了解下...2020-06-25
  • c#基础知识---委托,匿名函数,lambda

    这篇文章主要介绍了c# 委托,匿名函数,lambda的相关知识,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-11-03
  • C# 通过反射获取类型的字段值及给字段赋值的操作

    这篇文章主要介绍了C# 通过反射获取类型的字段值及给字段赋值的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-19
  • 解决Keras 中加入lambda层无法正常载入模型问题

    这篇文章主要介绍了解决Keras 中加入lambda层无法正常载入模型问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-17
  • .NET/C#如何使用反射注册事件详解

    反射是.NET中的重要机制,下面这篇文章主要给大家介绍了关于.NET/C#如何使用反射注册事件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧...2021-09-22
  • asp.net反射简单应用实例

    这篇文章主要介绍了asp.net反射简单应用,结合完整实例形式分析了asp.net反射的原理与简单使用方法,需要的朋友可以参考下...2021-09-22
  • php面向对象之反射功能与用法分析

    这篇文章主要介绍了php面向对象之反射功能与用法,结合实例形式简单分析了php5面向对象反射的概念及具体用法,需要的朋友可以参考下...2017-04-03
  • 深入解析Java反射之基础篇

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,这篇文章主要给大家介绍了关于Java反射之基础篇的相关资料,需要的朋友可以参考下...2021-11-18
  • 关于C#基础知识回顾--反射(二)

    其实说白了,反射就是能知道我们未知类型的类型信息这么一个东西.没什么神秘可讲!反射的核心是System.Type。System.Type包含了很多属性和方法,使用这些属性和方法可以在运行时得到类型信息...2020-06-25
  • C++反射的一种实现方法详解

    这篇文章主要给大家介绍了关于C++反射的一种实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C++具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-04-25
  • C++Lambda表达式详解

    这篇文章主要介绍了C++中的Lambda表达式详解,本文讲解了基本语法、Lambda的使用等内容,需要的朋友可以参考下,希望能够给你带来帮助...2021-10-15
  • C#泛型和反射实例解析

    这篇文章主要介绍了C#泛型和反射实例解析,对于C#初学者理解泛型和反射有很好的帮助借鉴作用,需要的朋友可以参考下...2020-06-25
  • C# 表达式树Expression Trees的知识梳理

    本篇文章主要介绍了表达式树 Expression Trees的基础知识:Lambda 表达式创建表达式树;API 创建表达式树;编译表达式树;执行表达式树;修改表达式树等等,具有一定的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • Asp.net中使用DapperExtensions和反射来实现一个通用搜索

    这篇文章主要介绍了Asp.net中使用DapperExtensions和反射来实现一个通用搜索功能,非常不错,具有参考解决价值,需要的朋友可以参考下...2021-09-22
  • C# IQueryable<T>揭开表达式树的神秘面纱

    这篇文章主要介绍了C# IQueryable<T>表达式树,对IQueryable<T>感兴趣的同学,必须要仔细看一下...2021-04-26
  • C# lambda表达式原理定义及实例详解

    这篇文章主要介绍了C# lambda表达式原理定义及实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-03
  • java 利用反射获取内部类静态成员变量的值操作

    这篇文章主要介绍了java 利用反射获取内部类静态成员变量的值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-04
  • Lambda表达式原理及示例

    这篇文章主要介绍了Java8中的新特性Lambda表达式,文中的示例可以帮助大家快速了解该特性,感兴趣的小伙伴可以一起学习...2021-08-17