Condividi tramite


Expression.Invoke Method (Expression, IEnumerable<Expression>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function Invoke ( _
    expression As Expression, _
    arguments As IEnumerable(Of Expression) _
) As InvocationExpression
public static InvocationExpression Invoke(
    Expression expression,
    IEnumerable<Expression> arguments
)

Parameters

Return Value

Type: System.Linq.Expressions.InvocationExpression
An InvocationExpression that applies the specified delegate or lambda expression to the provided arguments.

Exceptions

Exception Condition
ArgumentNullException

expression is nulla null reference (Nothing in Visual Basic).

ArgumentException

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.

InvalidOperationException

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

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 nulla null reference (Nothing in Visual Basic). Otherwise, it contains the same elements as arguments except that some of these Expression objects may be quoted.

NoteNote:

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.

Examples

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

Dim largeSumTest As System.Linq.Expressions.Expression(Of System.Func(Of Integer, Integer, Boolean)) = _
    Function(num1, num2) (num1 + num2) > 1000

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

outputBlock.Text &= invocationExpression.ToString() & vbCrLf

' This code produces the following output:
'
' Invoke((num1, num2) => ((num1 + num2) > 1000),539,281)
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));

outputBlock.Text += invocationExpression.ToString() + "\n";

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.