Expression<TDelegate>.Compile Method

Definition

Overloads

Compile()

Compiles the lambda expression described by the expression tree into executable code and produces a delegate that represents the lambda expression.

Compile(Boolean)

Compiles the lambda expression described by the expression tree into interpreted or compiled code and produces a delegate that represents the lambda expression.

Compile(DebugInfoGenerator)

Produces a delegate that represents the lambda expression.

Compile()

Compiles the lambda expression described by the expression tree into executable code and produces a delegate that represents the lambda expression.

public:
 TDelegate Compile();
public TDelegate Compile ();
override this.Compile : unit -> 'Delegate
Public Function Compile () As TDelegate

Returns

TDelegate

A delegate of type TDelegate that represents the compiled lambda expression described by the Expression<TDelegate>.

Examples

The following code example demonstrates how Compile is used to execute an expression tree.


// Lambda expression as data in the form of an expression tree.
System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
// Compile the expression tree into executable code.
Func<int, bool> deleg = expr.Compile();
// Invoke the method and print the output.
Console.WriteLine("deleg(4) = {0}", deleg(4));

/*  This code produces the following output:

    deleg(4) = True
*/

' Lambda expression as data in the form of an expression tree.
Dim expression As System.Linq.Expressions.Expression(Of Func(Of Integer, Boolean)) = Function(ByVal i) i < 5
' Compile the expression tree into executable code.
Dim deleg As Func(Of Integer, Boolean) = expression.Compile()
' Invoke the method and print the output.
MsgBox(String.Format("deleg(4) = {0}", deleg(4)))

' This code produces the following output:
'
' deleg(4) = True

Remarks

The Compile method produces a delegate of type TDelegate at runtime. When that delegate is executed, it has the behavior described by the semantics of the Expression<TDelegate>.

The Compile method can be used to obtain the value of any expression tree. First, create a lambda expression that has the expression as its body by using the Lambda method. Then call Compile to obtain a delegate, and execute the delegate to obtain the value of the expression.

Applies to

Compile(Boolean)

Compiles the lambda expression described by the expression tree into interpreted or compiled code and produces a delegate that represents the lambda expression.

public:
 TDelegate Compile(bool preferInterpretation);
public TDelegate Compile (bool preferInterpretation);
override this.Compile : bool -> 'Delegate
Public Function Compile (preferInterpretation As Boolean) As TDelegate

Parameters

preferInterpretation
Boolean

true to indicate that the expression should be compiled to an interpreted form, if it is available; false otherwise.

Returns

TDelegate

A delegate that represents the compiled lambda expression described by the Expression<TDelegate>.

Applies to

Compile(DebugInfoGenerator)

Produces a delegate that represents the lambda expression.

public:
 TDelegate Compile(System::Runtime::CompilerServices::DebugInfoGenerator ^ debugInfoGenerator);
public TDelegate Compile (System.Runtime.CompilerServices.DebugInfoGenerator debugInfoGenerator);
override this.Compile : System.Runtime.CompilerServices.DebugInfoGenerator -> 'Delegate
Public Function Compile (debugInfoGenerator As DebugInfoGenerator) As TDelegate

Parameters

debugInfoGenerator
DebugInfoGenerator

Debugging information generator used by the compiler to mark sequence points and annotate local variables.

Returns

TDelegate

A delegate containing the compiled version of the lambda.

Applies to