c#反射最佳化 運算式樹狀架構

來源:互聯網
上載者:User

標籤:style   turn   code   轉換   eth   bsp   ring   res   type   

   public static class FastRefelect    {        static Type pType;        static object cmp;        static Type mO;        static Type mT;        static Type mType;        static MethodInfo m;        static int i = 0;        static int k = 0;        public static Func<T, MethodInfo, object, object> GetSetDelegate<T>(MethodInfo m, Type type)        {            Action<T, MethodInfo, object> set = null;            if (k == 0)            {                var param_obj = Expression.Parameter(mT, "obj");                var param_val = Expression.Parameter(mO, "val");                var param_m = Expression.Parameter(mType, "m");                var body_val = Expression.Convert(param_val, type);                var body = Expression.Call(param_obj, m, body_val);                set = Expression.Lambda<Action<T, MethodInfo, object>>(body, param_obj, param_m, param_val).Compile();                cmp = set;                k++;            }            else            {                set = (Action<T, MethodInfo, object>)cmp;            }            return (instance, method, v) =>            {                set(instance, method, v);                return null;            };        }        public static void FastSetValue<T>(this PropertyInfo property, T t, object value)        {            if (i == 0)            {                pType = property.PropertyType;                mO = typeof(object);                mT = typeof(T);                mType = typeof(MethodInfo);                m = property.GetSetMethod();                i++;            }            GetSetDelegate<T>(m, pType)(t, m, value);        }        public static object FastGetValue<T>(this object obj,System.Linq.Expressions.Expression<Func<T,string>> TProperty)        {            var type = obj.GetType();            //做緩衝            var p = type.GetProperty(TProperty.Body.ToString().Split(‘.‘)[1]);            //lambda的參數u             var param_obj = Expression.Parameter(typeof(object), "obj");            //類型轉換            var convert_obj = Expression.Convert(param_obj, type);            //lambda的方法體 ((MyMath)obj).Age            var pGetter = Expression.Property(convert_obj, p);            //對傳回值進行類型轉換            var returnObj = Expression.Convert(pGetter, typeof(object));            //編譯lambda             var getValue = Expression.Lambda<Func<object, object>>(returnObj, param_obj).Compile();            return getValue(obj);        }    }

 

c#反射最佳化 運算式樹狀架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.