using System; using System.Linq.Expressions; using System.Reflection; namespace DymWebForm.Common.Reflection { internal static class Extensions { public static Func GenerateGetterFunc(this PropertyInfo pi) { //p=> ((pi.DeclaringType)p). var expParamPo = Expression.Parameter(typeof(object), "p"); var expParamPc = Expression.Convert(expParamPo, pi.DeclaringType); var expMma = Expression.MakeMemberAccess( expParamPc , pi ); var expMmac = Expression.Convert(expMma, typeof(object)); var exp = Expression.Lambda>(expMmac, expParamPo); return exp.Compile(); } public static Action GenerateSetterAction(this PropertyInfo pi) { //p=> ((pi.DeclaringType)p).=(pi.PropertyType)v var expParamPo = Expression.Parameter(typeof(object), "p"); var expParamPc = Expression.Convert(expParamPo, pi.DeclaringType); var expParamV = Expression.Parameter(typeof(object), "v"); var expParamVc = Expression.Convert(expParamV, pi.PropertyType); var expMma = Expression.Call( expParamPc , pi.GetSetMethod() , expParamVc ); var exp = Expression.Lambda>(expMma, expParamPo, expParamV); return exp.Compile(); } } }