Expression<TDelegate>.Compile 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| 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 為代表由 描述的編譯 lambda 表達 Expression<TDelegate>式的代理。
範例
以下程式碼範例示範如何使用 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 否則,
傳回
一個代表由 描述 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
編譯器用於標記序列點及註解局部變數的除錯資訊產生器。
傳回
一個包含 lambda 編譯版本的代理。