当前位置: 移动技术网 > IT编程>开发语言>c# > 利用lambda表达式树优化反射详解

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

前言

本节重点不讲反射机制,而是讲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技术优化反射,会比表达式略快一点。

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

总结

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网