예제 #1
0
        public static Func <TArg, object> CreateConstructor <TArg>(this Type @this)
        {
            ConstructorInfo constructorInfo = @this.GetConstructor(typeof(TArg));

            if (constructorInfo == null)
            {
                return(null);
            }
            ParameterExpression arg = Expression.Parameter(typeof(TArg));

            return(Expression.Lambda <Func <TArg, object> >(Expression.New(constructorInfo, arg.Collect()), true, arg.Collect()).Compile());
        }
예제 #2
0
        private static Action <object, Delegate> CreateEventMethod(MethodInfo methodInfo)
        {
            ParameterExpression thisParameter  = Expression.Parameter(typeof(object));
            UnaryExpression     convertThis    = Expression.Convert(thisParameter, methodInfo.DeclaringType);
            ParameterExpression valueParamater = Expression.Parameter(typeof(Delegate));
            UnaryExpression     convertValue   = Expression.Convert(valueParamater, methodInfo.GetParameters()[0].ParameterType);

            return(Expression.Lambda <Action <object, Delegate> >(Expression.Call(convertThis, methodInfo, Enumerable.Repeat(convertValue, 1)), false, thisParameter.Collect().AndThis(valueParamater)).Compile());
        }
예제 #3
0
        public static Func <object, TArg, TResult> GetInstanceFunctionFunction <TArg, TResult>(this Type @this, string functionName)
        {
            ParameterExpression thisParameter = Expression.Parameter(typeof(object));
            ParameterExpression argParameter  = Expression.Parameter(typeof(TArg));

            MethodInfo[] methodInfos = @this.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            MethodInfo   methodInfo  = null;

            for (int i = 0; i < methodInfos.Length; i++, methodInfo = null)
            {
                methodInfo = methodInfos[i];
                if (methodInfo.Name == functionName)
                {
                    ParameterInfo[] parameterInfos = methodInfo.GetParameters();
                    if (parameterInfos.Length == 1 && parameterInfos[0].ParameterType.IsSameAs(typeof(TArg)))
                    {
                        break;
                    }
                }
            }
            if (methodInfo == null)
            {
                return(null);
            }
            return(Expression.Lambda <Func <object, TArg, TResult> >(Expression.Call(Expression.Convert(thisParameter, @this), methodInfo, Enumerable.Repeat(argParameter, 1)), true, thisParameter.Collect().AndThis(argParameter)).Compile());
        }