Expression.Call Method

Definition

Overloads

Call(MethodInfo, Expression, Expression, Expression, Expression, Expression)

Creates a MethodCallExpression that represents a call to a static method that takes five arguments.

Call(Expression, MethodInfo, Expression, Expression, Expression)

Creates a MethodCallExpression that represents a call to a method that takes three arguments.

Call(Type, String, Type[], Expression[])

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method by calling the appropriate factory method.

Call(MethodInfo, Expression, Expression, Expression)

Creates a MethodCallExpression that represents a call to a static method that takes three arguments.

Call(Expression, String, Type[], Expression[])

Creates a MethodCallExpression that represents a call to a method by calling the appropriate factory method.

Call(Expression, MethodInfo, Expression, Expression)

Creates a MethodCallExpression that represents a call to a method that takes two arguments.

Call(MethodInfo, Expression, Expression, Expression, Expression)

Creates a MethodCallExpression that represents a call to a static method that takes four arguments.

Call(Expression, MethodInfo, Expression[])

Creates a MethodCallExpression that represents a call to a method that takes arguments.

Call(Expression, MethodInfo, IEnumerable<Expression>)

Creates a MethodCallExpression that represents a call to a method that takes arguments.

Call(MethodInfo, Expression[])

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method that has arguments.

Call(MethodInfo, Expression)

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method that takes one argument.

Call(MethodInfo, IEnumerable<Expression>)

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method.

Call(Expression, MethodInfo)

Creates a MethodCallExpression that represents a call to a method that takes no arguments.

Call(MethodInfo, Expression, Expression)

Creates a MethodCallExpression that represents a call to a static method that takes two arguments.

Call(MethodInfo, Expression, Expression, Expression, Expression, Expression)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static method that takes five arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2, System.Linq.Expressions.Expression arg3, System.Linq.Expressions.Expression arg4);

Parameters

method
MethodInfo

A MethodInfo to set the Method property equal to.

arg0
Expression

The Expression that represents the first argument.

arg1
Expression

The Expression that represents the second argument.

arg2
Expression

The Expression that represents the third argument.

arg3
Expression

The Expression that represents the fourth argument.

arg4
Expression

The Expression that represents the fifth argument.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Exceptions

method is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(Expression, MethodInfo, Expression, Expression, Expression)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a method that takes three arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression? instance, System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2);

Parameters

instance
Expression

An Expression that specifies the instance for an instance call. (pass null for a static (Shared in Visual Basic) method).

method
MethodInfo

The MethodInfo that represents the target method.

arg0
Expression

The Expression that represents the first argument.

arg1
Expression

The Expression that represents the second argument.

arg2
Expression

The Expression that represents the third argument.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(Type, String, Type[], Expression[])

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method by calling the appropriate factory method.

C#
public static System.Linq.Expressions.MethodCallExpression Call(Type type, string methodName, Type[] typeArguments, params System.Linq.Expressions.Expression[] arguments);
C#
public static System.Linq.Expressions.MethodCallExpression Call(Type type, string methodName, Type[]? typeArguments, params System.Linq.Expressions.Expression[]? arguments);

Parameters

type
Type

The type that contains the specified static (Shared in Visual Basic) method.

methodName
String

The name of the method.

typeArguments
Type[]

An array of Type objects that specify the type parameters of the generic method. This argument should be null when methodName specifies a non-generic method.

arguments
Expression[]

An array of Expression objects that represent the arguments to the method.

Returns

A MethodCallExpression that has the NodeType property equal to Call, the Method property set to the MethodInfo that represents the specified static (Shared in Visual Basic) method, and the Arguments property set to the specified arguments.

Exceptions

type or methodName is null.

No method whose name is methodName, whose type parameters match typeArguments, and whose parameter types match arguments is found in type or its base types.

-or-

More than one method whose name is methodName, whose type parameters match typeArguments, and whose parameter types match arguments is found in type or its base types.

Remarks

The Type property of the resulting MethodCallExpression is equal to the return type of the method denoted by methodName. The Object property is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(MethodInfo, Expression, Expression, Expression)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static method that takes three arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2);

Parameters

method
MethodInfo

A MethodInfo to set the Method property equal to.

arg0
Expression

The Expression that represents the first argument.

arg1
Expression

The Expression that represents the second argument.

arg2
Expression

The Expression that represents the third argument.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Exceptions

method is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(Expression, String, Type[], Expression[])

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a method by calling the appropriate factory method.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression instance, string methodName, Type[] typeArguments, params System.Linq.Expressions.Expression[] arguments);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression instance, string methodName, Type[]? typeArguments, params System.Linq.Expressions.Expression[]? arguments);

Parameters

instance
Expression

An Expression whose Type property value will be searched for a specific method.

methodName
String

The name of the method.

typeArguments
Type[]

An array of Type objects that specify the type parameters of the generic method. This argument should be null when methodName specifies a non-generic method.

arguments
Expression[]

An array of Expression objects that represents the arguments to the method.

Returns

A MethodCallExpression that has the NodeType property equal to Call, the Object property equal to instance, Method set to the MethodInfo that represents the specified instance method, and Arguments set to the specified arguments.

Exceptions

instance or methodName is null.

No method whose name is methodName, whose type parameters match typeArguments, and whose parameter types match arguments is found in instance.Type or its base types.

-or-

More than one method whose name is methodName, whose type parameters match typeArguments, and whose parameter types match arguments is found in instance.Type or its base types.

Remarks

The Type property of the resulting MethodCallExpression is equal to the return type of the method denoted by methodName.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(Expression, MethodInfo, Expression, Expression)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a method that takes two arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression? instance, System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1);

Parameters

instance
Expression

An Expression that specifies the instance for an instance call. (pass null for a static (Shared in Visual Basic) method).

method
MethodInfo

The MethodInfo that represents the target method.

arg0
Expression

The Expression that represents the first argument.

arg1
Expression

The Expression that represents the second argument.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Examples

The following code example shows how to create an expression that calls an instance method that has two arguments.

C#
// Add the following directive to your file:
// using System.Linq.Expressions;
public class SampleClass
{
    public int AddIntegers(int arg1, int arg2)
    {
        return arg1 + arg2;
    }
}

static public void TestCall()
{
    // This expression represents a call to an instance method that has two arguments.
    // The first argument is an expression that creates a new object of the specified type.
    Expression callExpr = Expression.Call(
        Expression.New(typeof(SampleClass)),
        typeof(SampleClass).GetMethod("AddIntegers", new Type[] { typeof(int), typeof(int) }),
        Expression.Constant(1),
        Expression.Constant(2)
        );

    // Print out the expression.
    Console.WriteLine(callExpr.ToString());

    // The following statement first creates an expression tree,
    // then compiles it, and then executes it.
    Console.WriteLine(Expression.Lambda<Func<int>>(callExpr).Compile()());

    // This code example produces the following output:
    //
    // new SampleClass().AddIntegers(1, 2)
    // 3
}

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(MethodInfo, Expression, Expression, Expression, Expression)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static method that takes four arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2, System.Linq.Expressions.Expression arg3);

Parameters

method
MethodInfo

A MethodInfo to set the Method property equal to.

arg0
Expression

The Expression that represents the first argument.

arg1
Expression

The Expression that represents the second argument.

arg2
Expression

The Expression that represents the third argument.

arg3
Expression

The Expression that represents the fourth argument.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Exceptions

method is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(Expression, MethodInfo, Expression[])

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a method that takes arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, params System.Linq.Expressions.Expression[] arguments);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression? instance, System.Reflection.MethodInfo method, params System.Linq.Expressions.Expression[]? arguments);

Parameters

instance
Expression

An Expression that specifies the instance for an instance method call (pass null for a static (Shared in Visual Basic) method).

method
MethodInfo

A MethodInfo to set the Method property equal to.

arguments
Expression[]

An array of Expression objects to use to populate the Arguments collection.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object, Method, and Arguments properties set to the specified values.

Exceptions

method is null.

-or-

instance is null and method represents an instance method.

-or-

arguments is not null and one or more of its elements is null.

instance.Type is not assignable to the declaring type of the method represented by method.

-or-

The number of elements in arguments does not equal the number of parameters for the method represented by method.

-or-

One or more of the elements of arguments is not assignable to the corresponding parameter for the method represented by method.

Remarks

To represent a call to a static (Shared in Visual Basic) method, pass in null for the instance parameter when you call this method, or call Call instead.

If method represents an instance method, the Type property of instance must be assignable to the declaring type of the method represented by method.

If arguments is not null, it must have the same number of elements as the number of parameters for the method represented by method. Each element in arguments must not be null and must be assignable to the corresponding parameter of method, possibly after quoting.

Note

An element will be quoted only if the corresponding method parameter is of type Expression. Quoting means the element is wrapped in a Quote node. The resulting node is a UnaryExpression whose Operand property is the element of arguments.

The Arguments property of the resulting MethodCallExpression is empty if arguments is null. Otherwise, it contains the same elements as arguments, some of which may be quoted.

The Type property of the resulting MethodCallExpression is equal to the return type of the method represented by method.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(Expression, MethodInfo, IEnumerable<Expression>)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a method that takes arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression? instance, System.Reflection.MethodInfo method, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments);

Parameters

instance
Expression

An Expression to set the Object property equal to (pass null for a static (Shared in Visual Basic) method).

method
MethodInfo

A MethodInfo to set the Method property equal to.

arguments
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Arguments collection.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object, Method, and Arguments properties set to the specified values.

Exceptions

method is null.

-or-

instance is null and method represents an instance method.

instance.Type is not assignable to the declaring type of the method represented by method.

-or-

The number of elements in arguments does not equal the number of parameters for the method represented by method.

-or-

One or more of the elements of arguments is not assignable to the corresponding parameter for the method represented by method.

Remarks

To represent a call to a static (Shared in Visual Basic) method, pass in null for the instance parameter when you call this method, or call Call instead.

If method represents an instance method, the Type property of instance must be assignable to the declaring type of the method represented by method.

If arguments is not null, it must have the same number of elements as the number of parameters for the method represented by method. Each element in arguments must not be null and must be assignable to the corresponding parameter of method, possibly after quoting.

Note

An element will be quoted only if the corresponding method parameter is of type Expression. Quoting means the element is wrapped in a Quote node. The resulting node is a UnaryExpression whose Operand property is the element of arguments.

The Arguments property of the resulting MethodCallExpression is empty if arguments is null. Otherwise, it contains the same elements as arguments, some of which may be quoted.

The Type property of the resulting MethodCallExpression is equal to the return type of the method represented by method.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(MethodInfo, Expression[])

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method that has arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, params System.Linq.Expressions.Expression[] arguments);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, params System.Linq.Expressions.Expression[]? arguments);

Parameters

method
MethodInfo

A MethodInfo that represents a static (Shared in Visual Basic) method to set the Method property equal to.

arguments
Expression[]

An array of Expression objects to use to populate the Arguments collection.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Method and Arguments properties set to the specified values.

Exceptions

method is null.

The number of elements in arguments does not equal the number of parameters for the method represented by method.

-or-

One or more of the elements of arguments is not assignable to the corresponding parameter for the method represented by method.

Remarks

If arguments is not null, it must have the same number of elements as the number of parameters for the method represented by method. Each element in arguments must not be null and must be assignable to the corresponding parameter of method, possibly after quoting.

Note

An element will be quoted only if the corresponding method parameter is of type Expression. Quoting means the element is wrapped in a Quote node. The resulting node is a UnaryExpression whose Operand property is the element of arguments.

The Arguments property of the resulting MethodCallExpression is empty if arguments is null. Otherwise, it contains the same elements as arguments, some of which may be quoted.

The Type property of the resulting MethodCallExpression is equal to the return type of the method represented by method. The Object property is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(MethodInfo, Expression)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method that takes one argument.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0);

Parameters

method
MethodInfo

A MethodInfo to set the Method property equal to.

arg0
Expression

The Expression that represents the first argument.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Exceptions

method is null.

Examples

The following example demonstrates how to create an expression that calls a static (Shared in Visual Basic) method that takes one argument.

C#
// Add the following directive to your file:
// using System.Linq.Expressions;

public class SampleClass
{
    public static int Increment(int arg1)
    {
        return arg1 + 1;
    }
}

static public void TestCall()
{

    //This expression represents a call to an instance method with one argument.
    Expression callExpr = Expression.Call(
                            typeof(SampleClass).GetMethod("Increment"),
                            Expression.Constant(2)
                        );

    // Print out the expression.
    Console.WriteLine(callExpr.ToString());

    // The following statement first creates an expression tree,
    // then compiles it, and then executes it.
    Console.WriteLine(Expression.Lambda<Func<int>>(callExpr).Compile()());

    // This code example produces the following output:
    //
    // Increment(2)
    // 3
}

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(MethodInfo, IEnumerable<Expression>)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static (Shared in Visual Basic) method.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments);

Parameters

method
MethodInfo

The MethodInfo that represents the target method.

arguments
IEnumerable<Expression>

A collection of Expression that represents the call arguments.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(Expression, MethodInfo)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a method that takes no arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method);
C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Linq.Expressions.Expression? instance, System.Reflection.MethodInfo method);

Parameters

instance
Expression

An Expression that specifies the instance for an instance method call (pass null for a static (Shared in Visual Basic) method).

method
MethodInfo

A MethodInfo to set the Method property equal to.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Exceptions

method is null.

-or-

instance is null and method represents an instance method.

instance.Type is not assignable to the declaring type of the method represented by method.

Examples

The following code example shows how to create an expression that calls a method without arguments.

C#
// Add the following directive to your file:
// using System.Linq.Expressions;

// This expression represents a call to an instance method without arguments.
Expression callExpr = Expression.Call(
    Expression.Constant("sample string"), typeof(String).GetMethod("ToUpper", new Type[] { }));

// Print out the expression.
Console.WriteLine(callExpr.ToString());

// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(Expression.Lambda<Func<String>>(callExpr).Compile()());

// This code example produces the following output:
//
// "sample string".ToUpper
// SAMPLE STRING

Remarks

To represent a call to a static (Shared in Visual Basic) method, pass in null for the instance parameter when you call this method.

If method represents an instance method, the Type property of instance must be assignable to the declaring type of the method represented by method.

The Arguments property of the resulting MethodCallExpression is empty. The Type property is equal to the return type of the method represented by method.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Call(MethodInfo, Expression, Expression)

Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs
Source:
MethodCallExpression.cs

Creates a MethodCallExpression that represents a call to a static method that takes two arguments.

C#
public static System.Linq.Expressions.MethodCallExpression Call(System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1);

Parameters

method
MethodInfo

A MethodInfo to set the Method property equal to.

arg0
Expression

The Expression that represents the first argument.

arg1
Expression

The Expression that represents the second argument.

Returns

A MethodCallExpression that has the NodeType property equal to Call and the Object and Method properties set to the specified values.

Exceptions

method is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0