System.Linq.Expressions.Expression.Call C# (CSharp) Method

Call() public static method

Creates a MethodCallExpression that represents a method call.
/// is null.-or- is null and represents an instance method. /// .Type is not assignable to the declaring type of the method represented by .-or-The number of elements in does not equal the number of parameters for the method represented by .-or-One or more of the elements of is not assignable to the corresponding parameter for the method represented by .
public static Call ( Expression instance, MethodInfo method, IEnumerable arguments ) : MethodCallExpression
instance Expression An to set the property equal to (pass null for a static (Shared in Visual Basic) method).
method System.Reflection.MethodInfo A to set the property equal to.
arguments IEnumerable An that contains objects to use to populate the collection.
return MethodCallExpression
        public static MethodCallExpression Call(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
        {
            IReadOnlyList<Expression> argumentList = arguments as IReadOnlyList<Expression>;

            if (argumentList != null)
            {
                int argCount = argumentList.Count;

                switch (argCount)
                {
                    case 0:
                        return Call(instance, method);
                    case 1:
                        return Call(instance, method, argumentList[0]);
                    case 2:
                        return Call(instance, method, argumentList[0], argumentList[1]);
                    case 3:
                        return Call(instance, method, argumentList[0], argumentList[1], argumentList[2]);
                }

                if (instance == null)
                {
                    switch (argCount)
                    {
                        case 4:
                            return Call(method, argumentList[0], argumentList[1], argumentList[2], argumentList[3]);
                        case 5:
                            return Call(method, argumentList[0], argumentList[1], argumentList[2], argumentList[3], argumentList[4]);
                    }
                }
            }

            ContractUtils.RequiresNotNull(method, nameof(method));

            ReadOnlyCollection<Expression> argList = arguments.ToReadOnly();

            ValidateMethodInfo(method, nameof(method));
            ValidateStaticOrInstanceMethod(instance, method);
            ValidateArgumentTypes(method, ExpressionType.Call, ref argList, nameof(method));

            if (instance == null)
            {
                return new MethodCallExpressionN(method, argList);
            }
            else
            {
                return new InstanceMethodCallExpressionN(method, instance, argList);
            }
        }

Same methods

Expression::Call ( Expression instance, MethodInfo method ) : MethodCallExpression
Expression::Call ( Expression instance, MethodInfo method, Expression arg0 ) : MethodCallExpression
Expression::Call ( Expression instance, MethodInfo method, Expression arg0, Expression arg1 ) : MethodCallExpression
Expression::Call ( Expression instance, MethodInfo method, Expression arg0, Expression arg1, Expression arg2 ) : MethodCallExpression
Expression::Call ( Expression instance, string methodName, Type typeArguments ) : MethodCallExpression
Expression::Call ( MethodInfo method ) : MethodCallExpression
Expression::Call ( MethodInfo method, Expression arg0 ) : MethodCallExpression
Expression::Call ( MethodInfo method, Expression arg0, Expression arg1 ) : MethodCallExpression
Expression::Call ( MethodInfo method, Expression arg0, Expression arg1, Expression arg2 ) : MethodCallExpression
Expression::Call ( MethodInfo method, Expression arg0, Expression arg1, Expression arg2, Expression arg3 ) : MethodCallExpression
Expression::Call ( MethodInfo method, Expression arg0, Expression arg1, Expression arg2, Expression arg3, Expression arg4 ) : MethodCallExpression
Expression::Call ( MethodInfo method, IEnumerable arguments ) : MethodCallExpression
Expression::Call ( Type type, string methodName, Type typeArguments ) : MethodCallExpression

Usage Example

コード例 #1
0
        private static Func <int[], int[]> GenerateCopyExpression()
        {
            var ctor = typeof(int[]).GetConstructor(new[] { typeof(int) });
            var get  = typeof(int[]).GetMethod("Get", new[] { typeof(int) });
            var set  = typeof(int[]).GetMethod("Set", new[] { typeof(int), typeof(int) });

            var p1     = Exp.Parameter(typeof(int[]));
            var v1     = Exp.Variable(typeof(int[]));
            var v2     = Exp.Variable(typeof(int));
            var @break = Exp.Label();

            var block = Exp.Block(
                new[] { v1, v2 },
                Exp.Assign(v1, Exp.New(ctor, Exp.Property(p1, "Length"))),
                Exp.Assign(v2, Exp.Constant(0)),
                Exp.Loop(
                    Exp.IfThenElse(
                        Exp.LessThan(v2, Exp.Property(p1, "Length")),
                        Exp.Block(
                            Exp.Call(v1, set, v2, Exp.Call(p1, get, v2)),
                            Exp.AddAssign(v2, Exp.Constant(1))
                            ),
                        Exp.Break(@break)
                        ),
                    @break),
                v1
                );

            return(Exp.Lambda <Func <int[], int[]> >(block, new ParameterExpression[] { p1 }).Compile());
        }
All Usage Examples Of System.Linq.Expressions.Expression::Call
Expression