C # Reflection-optimized expression tree

Source: Internet
Author: User

    Public Static classFastrefelect {StaticType PType; Static ObjectCMP; StaticType MO; StaticType MT; StaticType Mtype; StaticMethodInfo m; Static inti =0; Static intK =0;  Public StaticFunc<t, MethodInfo,Object,Object> getsetdelegate<t>(MethodInfo m, type type) {Action<t, MethodInfo,Object>Set=NULL; if(k = =0)            {                varParam_obj = Expression.parameter (MT,"obj"); varParam_val = Expression.parameter (MO,"Val"); varParam_m = Expression.parameter (Mtype,"m"); varBody_val =Expression.convert (param_val, type); varBODY =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 voidFastsetvalue<t> ( ThisPropertyInfo property, T T,Objectvalue) {            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 ObjectFastgetvalue<t> ( This ObjectObj,system.linq.expressions.expression<func<t,string>>Tproperty) {            varType =obj.            GetType (); //do cache            varp = type. GetProperty (TProperty.Body.ToString (). Split ('.')[1]); //the parameters of lambda u            varParam_obj = Expression.parameter (typeof(Object),"obj"); //Type Conversions            varConvert_obj =Expression.convert (param_obj, type); //the Method body ((MyMath) obj) of the lambda. Age            varPgetter =expression.property (Convert_obj, p); //type conversion of return values            varReturnobj = Expression.convert (Pgetter,typeof(Object)); //Compiling Lambda            varGetValue = expression.lambda<func<Object,Object>>(Returnobj, param_obj).            Compile (); returngetValue (obj); }    }

C # Reflection-optimized expression tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.