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