Прочитај на енглеском Уреди

Делите путем


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()

Source:
LambdaExpression.cs
Source:
LambdaExpression.cs
Source:
LambdaExpression.cs

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

C#
public TDelegate Compile();

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.

C#

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

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

.NET 10 и друге верзије
Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Compile(Boolean)

Source:
LambdaExpression.cs
Source:
LambdaExpression.cs
Source:
LambdaExpression.cs

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

C#
public TDelegate Compile(bool preferInterpretation);

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

.NET 10 и друге верзије
Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.6, 2.0, 2.1

Compile(DebugInfoGenerator)

Source:
LambdaExpression.cs
Source:
LambdaExpression.cs
Source:
LambdaExpression.cs

Produces a delegate that represents the lambda expression.

C#
public TDelegate Compile(System.Runtime.CompilerServices.DebugInfoGenerator debugInfoGenerator);

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

.NET 10 и друге верзије
Производ Верзије
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1