C#优化反射

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 反射
{
    class AnObject
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime Date { get; set; }

        public void Call(object o)
        {

        }
        public AnObject(int i)
        { 
        
        }
    }
}

 

//直接调用、反射调用、优化反射调用分别如下:

 

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 反射
{
    class Program
    {
        public delegate void CallBack(object obj);
        public delegate string ReadProperty();
        public const int TIMES = 1000000;
        static void Main(string[] args)
        {
            AnObject obj = new AnObject(1);
            obj.Call(null);
            DirectUse(obj,null);
            ReflectionUse(obj, null);
            better_ReflectionUse(obj, null);
            GenericDelegateUse(obj);
            Console.ReadKey();
        }

        //直接调用
        public static void DirectUse(AnObject o,object parameter)
        {
            var watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < TIMES; i++)
            {
             
                o.Call(parameter);
            }
            watch.Stop();
            Console.WriteLine("直接调用方法耗时:"+watch.ElapsedMilliseconds);
            watch.Restart();
         
            for (int p = 0; p < TIMES; p++)
            {
                var name = o.Name;
            }
            watch.Stop();
            Console.WriteLine("直接获取属性耗时:" + watch.ElapsedMilliseconds);
        }
        //反射调用
        public static void ReflectionUse(AnObject o, object parameter)
        {
            var watch = new Stopwatch();
            watch.Start();
             var methodInfo= o.GetType().GetMethod("Call");
             var parameters = new[] { parameter};
            for (int p = 0; p < TIMES; p++)
            {
                //反射慢,主要就是这部的“参数验证耗时”
                methodInfo.Invoke(o,parameters);
            }
            watch.Stop();
            Console.WriteLine("反射调用方法耗时:" + watch.ElapsedMilliseconds);
            watch.Restart();
            PropertyInfo propertyInfo=  o.GetType().GetProperty("Name");
            for (int p = 0; p < TIMES; p++)
            {
                //反射慢,主要就是这部的“参数验证耗时”
                var name = propertyInfo.GetValue(o);
            }
            watch.Stop();
            Console.WriteLine("反射获取属性耗时:" + watch.ElapsedMilliseconds);

         
        }

        //优化反射1(利用委托调用)
        public static void better_ReflectionUse(AnObject o, object parameter)
        {
            var watch = new Stopwatch();
            watch.Start();
            var methodInfo=o.GetType().GetMethod("Call");
            CallBack callBack = (CallBack)Delegate.CreateDelegate(typeof(CallBack), o, methodInfo);
            for (int p = 0; p < TIMES; p++)
            {
                callBack.Invoke(null);
            }
            watch.Stop();
            Console.WriteLine("优化后反射调用方法耗时:" + watch.ElapsedMilliseconds);
            watch.Restart();
            var readPropertyInfo = o.GetType().GetMethod("get_Name");
            ReadProperty ReadProperty_CallBack = (ReadProperty)Delegate.CreateDelegate(typeof(ReadProperty), o, readPropertyInfo);
            for (int p = 0; p < TIMES; p++)
            {
               string name= ReadProperty_CallBack.Invoke();
            }
            watch.Stop();
            Console.WriteLine("优化后反射调用属性耗时:" + watch.ElapsedMilliseconds);

        
        }


        #region 通用优化反射获取属性类

        public class GetterWarpper<T>
        {
            private readonly Func<T> _getter;
            public GetterWarpper(AnObject o,PropertyInfo p)
            {
                var m=p.GetGetMethod(true);
                _getter = (Func<T>)Delegate.CreateDelegate(typeof(Func<T>),o,m);
            }

            public T GetValue()
            {
                return _getter();
            }
        }


        public static void GenericDelegateUse(AnObject o)
        {
            var wt = new Stopwatch();
            wt.Start();
          var property=  typeof(AnObject).GetProperty("Name");
          var genricDel = new GetterWarpper<string>(o,property);
          string obj = "";
          for (int p = 0; p < TIMES; p++)
          {
             obj= genricDel.GetValue();
          }
          wt.Stop();
          Console.WriteLine("通用_优化后反射调用属性耗时:" + wt.ElapsedMilliseconds);

          wt.Restart();
          var property_age = typeof(AnObject).GetProperty("Age");
          var genricDel_age = new GetterWarpper<int>(o, property_age);
          int obj_age = 0;
          for (int p = 0; p < TIMES; p++)
          {
              obj_age = genricDel_age.GetValue();
          }
          wt.Stop();
          Console.WriteLine("通用_优化后反射调用属性耗时:" + wt.ElapsedMilliseconds);

        

        }
        #endregion

    }

 
}

      

 

 

 

 测试结果:

posted @ 2020-05-13 18:57  _MrZhu  阅读(287)  评论(0)    收藏  举报