Expression<TDelegate>.Compile 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| 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
返回
表示所Expression<TDelegate>描述的已编译 lambda 表达式的类型的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 否则。
返回
一个表示所描述的已编译 lambda 表达式的 Expression<TDelegate>委托。
适用于
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 编译版本的委托。