Expression.Invoke Method

Definition

Creates an InvocationExpression.

Overloads

Invoke(Expression, Expression[])

Creates an InvocationExpression that applies a delegate or lambda expression to a list of argument expressions.

Invoke(Expression, IEnumerable<Expression>)

Creates an InvocationExpression that applies a delegate or lambda expression to a list of argument expressions.

Invoke(Expression, Expression[])

Source:
InvocationExpression.cs
Source:
InvocationExpression.cs
Source:
InvocationExpression.cs

Creates an InvocationExpression that applies a delegate or lambda expression to a list of argument expressions.

C#
public static System.Linq.Expressions.InvocationExpression Invoke(System.Linq.Expressions.Expression expression, params System.Linq.Expressions.Expression[] arguments);
C#
public static System.Linq.Expressions.InvocationExpression Invoke(System.Linq.Expressions.Expression expression, params System.Linq.Expressions.Expression[]? arguments);

Parameters

expression
Expression

An Expression that represents the delegate or lambda expression to be applied.

arguments
Expression[]

An array of Expression objects that represent the arguments that the delegate or lambda expression is applied to.

Returns

An InvocationExpression that applies the specified delegate or lambda expression to the provided arguments.

Exceptions

expression is null.

expression.Type does not represent a delegate type or an Expression<TDelegate>.

-or-

The Type property of an element of arguments is not assignable to the type of the corresponding parameter of the delegate represented by expression.

arguments does not contain the same number of elements as the list of parameters for the delegate represented by expression.

Examples

The following example demonstrates how to use the Invoke(Expression, Expression[]) method to create an InvocationExpression that represents the invocation of a lambda expression with specified arguments.

C#
System.Linq.Expressions.Expression<Func<int, int, bool>> largeSumTest =
    (num1, num2) => (num1 + num2) > 1000;

// Create an InvocationExpression that represents applying
// the arguments '539' and '281' to the lambda expression 'largeSumTest'.
System.Linq.Expressions.InvocationExpression invocationExpression =
    System.Linq.Expressions.Expression.Invoke(
        largeSumTest,
        System.Linq.Expressions.Expression.Constant(539),
        System.Linq.Expressions.Expression.Constant(281));

Console.WriteLine(invocationExpression.ToString());

// This code produces the following output:
//
// Invoke((num1, num2) => ((num1 + num2) > 1000),539,281)

Remarks

The Type property of the resulting InvocationExpression represents the return type of the delegate that is represented by expression.Type.

The Arguments property of the resulting InvocationExpression is empty if arguments is null. Otherwise, it contains the same elements as arguments except that some of these Expression objects may be quoted.

Note

An element will be quoted only if the corresponding parameter of the delegate represented by expression 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.

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

Invoke(Expression, IEnumerable<Expression>)

Source:
InvocationExpression.cs
Source:
InvocationExpression.cs
Source:
InvocationExpression.cs

Creates an InvocationExpression that applies a delegate or lambda expression to a list of argument expressions.

C#
public static System.Linq.Expressions.InvocationExpression Invoke(System.Linq.Expressions.Expression expression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);
C#
public static System.Linq.Expressions.InvocationExpression Invoke(System.Linq.Expressions.Expression expression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression>? arguments);

Parameters

expression
Expression

An Expression that represents the delegate or lambda expression to be applied to.

arguments
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects that represent the arguments that the delegate or lambda expression is applied to.

Returns

An InvocationExpression that applies the specified delegate or lambda expression to the provided arguments.

Exceptions

expression is null.

expression.Type does not represent a delegate type or an Expression<TDelegate>.

-or-

The Type property of an element of arguments is not assignable to the type of the corresponding parameter of the delegate represented by expression.

arguments does not contain the same number of elements as the list of parameters for the delegate represented by expression.

Examples

The following example demonstrates how to use the Invoke(Expression, Expression[]) method to create an InvocationExpression that represents the invocation of a lambda expression with specified arguments.

C#
System.Linq.Expressions.Expression<Func<int, int, bool>> largeSumTest =
    (num1, num2) => (num1 + num2) > 1000;

// Create an InvocationExpression that represents applying
// the arguments '539' and '281' to the lambda expression 'largeSumTest'.
System.Linq.Expressions.InvocationExpression invocationExpression =
    System.Linq.Expressions.Expression.Invoke(
        largeSumTest,
        System.Linq.Expressions.Expression.Constant(539),
        System.Linq.Expressions.Expression.Constant(281));

Console.WriteLine(invocationExpression.ToString());

// This code produces the following output:
//
// Invoke((num1, num2) => ((num1 + num2) > 1000),539,281)

Remarks

The Type property of the resulting InvocationExpression represents the return type of the delegate that is represented by expression.Type.

The Arguments property of the resulting InvocationExpression is empty if arguments is null. Otherwise, it contains the same elements as arguments except that some of these Expression objects may be quoted.

Note

An element will be quoted only if the corresponding parameter of the delegate represented by expression 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.

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