コード例 #1
0
        /// <summary>
        /// Used for none type safe method calls that return values
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static Func <object, object[], object> CreateFastInvoke(this MethodInfo method)
        {
            ParameterExpression instanceParameter  = Expression.Parameter(typeof(object), "target");
            ParameterExpression argumentsParameter = Expression.Parameter(typeof(object[]), "arguments");
            Expression <Func <object, object[], object> > lambda = null;
            MethodCallExpression call = Expression.Call(
                Expression.Convert(instanceParameter, method.DeclaringType),
                method,
                FastInvokeUtilities.CreateParameterExpressions(method, argumentsParameter));

            if (method.ReturnType == typeof(void))
            {
                lambda = Expression.Lambda <Func <object, object[], object> >(
                    Expression.Block(call, Expression.Default(typeof(object))),
                    instanceParameter,
                    argumentsParameter);
            }
            else
            {
                lambda = Expression.Lambda <Func <object, object[], object> >(
                    call,
                    instanceParameter,
                    argumentsParameter);
            }

            return(lambda.Compile());
        }
コード例 #2
0
        /// <summary>
        /// Used when the type of the instance is known
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static Func <TInstance, object[], object> CreateFastInvoke <TInstance>(this MethodInfo method)
        {
            ParameterExpression instanceParameter  = Expression.Parameter(typeof(TInstance), "target");
            ParameterExpression argumentsParameter = Expression.Parameter(typeof(object[]), "arguments");
            Expression <Func <TInstance, object[], object> > lambda = null;

            // because we know the type we don't have to convert the instanceParameter like none type safe CreateAsFunc
            MethodCallExpression call = Expression.Call(
                instanceParameter,
                method,
                FastInvokeUtilities.CreateParameterExpressions(method, argumentsParameter));

            if (method.ReturnType == typeof(void))
            {
                // if method is void returns default value of object null
                lambda = Expression.Lambda <Func <TInstance, object[], object> >(
                    Expression.Block(call, Expression.Default(typeof(object))),
                    instanceParameter,
                    argumentsParameter);
            }
            else
            {
                // if method is void returns default value of object null
                lambda = Expression.Lambda <Func <TInstance, object[], object> >(
                    call,
                    instanceParameter,
                    argumentsParameter);
            }

            return(lambda.Compile());
        }
コード例 #3
0
        /// <summary>
        /// Used for constructors where the object type is known
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ctor">The ctor.</param>
        /// <returns></returns>
        public static Func <object[], TType> CreateAsFastInvoke <TType>(this ConstructorInfo constructorInfo)
        {
            ParameterExpression argumentsParameter = Expression.Parameter(typeof(object[]), "arguments");

            NewExpression call = Expression.New(
                constructorInfo,
                FastInvokeUtilities.CreateParameterExpressions(constructorInfo, argumentsParameter));

            Expression <Func <object[], TType> > lambda = Expression.Lambda <Func <object[], TType> >(
                call,
                argumentsParameter);

            return(lambda.Compile());
        }
コード例 #4
0
        /// <summary>
        /// Used for type safe method calls. Fastest way to invoke
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static Func <TInstance, object[], TReturn> CreateFastInvoke <TInstance, TReturn>(this MethodInfo method)
        {
            ParameterExpression instanceParameter  = Expression.Parameter(typeof(TInstance), "target");
            ParameterExpression argumentsParameter = Expression.Parameter(typeof(object[]), "arguments");

            // because we know the type we don't have to convert the instanceParameter like none type safe CreateAsFunc
            MethodCallExpression call = Expression.Call(
                instanceParameter,
                method,
                FastInvokeUtilities.CreateParameterExpressions(method, argumentsParameter));

            Expression <Func <TInstance, object[], TReturn> > lambda = Expression.Lambda <Func <TInstance, object[], TReturn> >(
                call,
                instanceParameter,
                argumentsParameter);

            return(lambda.Compile());
        }