Expression<TDelegate>.Compile 方法

定义

重载

Compile()

将表达式树描述的 lambda 表达式编译为可执行代码,并生成表示 lambda 表达式的委托。

Compile(Boolean)

将表达式树描述的 Lambda 表达式编译为已解释或已编译的代码,并生成表示该 Lambda 表达式的委托。

Compile(DebugInfoGenerator)

生成表示 lambda 表达式的委托。

Compile()

将表达式树描述的 lambda 表达式编译为可执行代码,并生成表示 lambda 表达式的委托。

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

返回

TDelegate

一个 TDelegate 类型的委托,它表示由 Expression<TDelegate> 描述的已编译的 lambda 表达式。

示例

下面的代码示例演示如何 Compile 用于执行表达式树。


// 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

注解

该方法 Compile 在运行时生成类型 TDelegate 委托。 执行该委托时,该委托的行为由该委托的 Expression<TDelegate>语义描述。

Compile该方法可用于获取任何表达式树的值。 首先,使用 Lambda 该方法创建一个 lambda 表达式,该表达式作为其主体。 然后调用 Compile 以获取委托,并执行委托以获取表达式的值。

适用于

Compile(Boolean)

将表达式树描述的 Lambda 表达式编译为已解释或已编译的代码,并生成表示该 Lambda 表达式的委托。

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

参数

preferInterpretation
Boolean

如果可用,则为 true,指示表达式应编译为已解释的形式;否则为 false

返回

TDelegate

一个委托,表示 Expression<TDelegate> 所描述的已编译 Lambda 表达式。

适用于

Compile(DebugInfoGenerator)

生成表示 lambda 表达式的委托。

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

参数

debugInfoGenerator
DebugInfoGenerator

编译器用于标记序列点并批注局部变量的调试信息生成器。

返回

TDelegate

包含 lambda 的已编译版本的委托。

适用于