本节重点不讲反射机制,而是讲lambda表达式树来替代反射中常用的获取属性和方法,来达到相同的效果但却比反射高效。
每个人都知道,用反射调用一个方法或者对属性执行SetValue和GetValue操作的时候都会比直接调用慢很多,这其中设计到CLR中内部的处理,不做深究。然而,我们在某些情况下又无法不使用反射,比如:在一个ORM框架中,你要将一个DataRow转化为一个对象,但你又不清楚该对象有什么属性,这时候你就需要写一个通用的泛型方法来处理,以下代码写得有点恶心,但不妨碍理解意思:
 
复制代码
     //将DataReader转化为一个对象
     private static T GetObj
(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> GetPropertyValueLambda = (Expression>)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> SetPropertyValueLambda = (Expression>)SetPropertyValueExp;
            //创建了属性Name的Get方法表达式和Set方法表达式,当然只是最简单的
            Func Getter = GetPropertyValueLambda.Compile(); 
            Action 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操作进行分装。
复制代码
         /// 
      /// 属性类,仿造反射中的PropertyInfo
    /// 
      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);
            }
        }
        /// 
           /// 获取对象的值
        /// 
          /// 
          /// 
           public Object GetValue(Object instance)
        {
            return getter?.Invoke(instance);
        }
        /// 
           /// 赋值操作
        /// 
          /// 
          /// 
           public void SetValue(Object instance, Object value)
        {
            this.setter?.Invoke(instance, value);
        }
        private static readonly ConcurrentDictionary securityCache = new ConcurrentDictionary();
        public static Core.Reflection.Property[] GetProperties(Type type)
        {
            return securityCache.GetOrAdd(type, t => t.GetProperties().Select(p => new Property(p)).ToArray());
        }
    }
     /// 
      /// 属性Get操作类
     /// 
      public class PropertyGetter
     {
        private readonly Func