Export (0) Print
Expand All

Expression.Invoke Method (Expression, Expression[])

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

public static InvocationExpression Invoke(
	Expression expression,
	params Expression[] arguments
)

Parameters

expression
Type: System.Linq.Expressions.Expression

An Expression to set the Expression property equal to.

arguments
Type: System.Linq.Expressions.Expression[]

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

Return Value

Type: System.Linq.Expressions.InvocationExpression
An InvocationExpression that has the NodeType property equal to Invoke and the Expression and Arguments properties set to the specified values.

ExceptionCondition
ArgumentNullException

expression is null.

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.

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.

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.

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.

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)

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft