Expression<TDelegate>.Compile 方法

定義

多載

Compile()

將運算式樹狀架構所描述的 Lambda 運算式編譯為可執行程式碼,並產生表示 Lambda 運算式的委派。

Compile(Boolean)

將運算式樹狀架構所描述的 Lambda 運算式,編譯成已解譯或已編譯的程式碼,並產生代表 Lambda 運算式的委派。

Compile(DebugInfoGenerator)

產生代表 Lambda 運算式的委派。

Compile()

來源:
LambdaExpression.cs
來源:
LambdaExpression.cs
來源:
LambdaExpression.cs

將運算式樹狀架構所描述的 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)

來源:
LambdaExpression.cs
來源:
LambdaExpression.cs
來源:
LambdaExpression.cs

將運算式樹狀架構所描述的 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)

來源:
LambdaExpression.cs
來源:
LambdaExpression.cs
來源:
LambdaExpression.cs

產生代表 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 的編譯版本。

適用於