Expression<TDelegate> Expression<TDelegate> Expression<TDelegate> Expression<TDelegate> Class

定義

將強類型 Lambda 運算式用樹狀架構形式的資料結構來表示。Represents a strongly typed lambda expression as a data structure in the form of an expression tree. 此類別無法獲得繼承。This class cannot be inherited.

generic <typename TDelegate>
public ref class Expression sealed : System::Linq::Expressions::LambdaExpression
public sealed class Expression<TDelegate> : System.Linq.Expressions.LambdaExpression
type Expression<'Delegate> = class
    inherit LambdaExpression
Public NotInheritable Class Expression(Of TDelegate)
Inherits LambdaExpression

類型參數

TDelegate

Expression<TDelegate> 所代表之委派的型別。The type of the delegate that the Expression<TDelegate> represents.

繼承
Expression<TDelegate>Expression<TDelegate>Expression<TDelegate>Expression<TDelegate>

範例

下列程式碼範例示範如何以委派的形式, 將 lambda 運算式表示為可執行程式碼, 並以運算式樹狀結構的形式呈現為數據。The following code example demonstrates how to represent a lambda expression both as executable code in the form of a delegate and as data in the form of an expression tree. 它也會示範如何使用Compile方法, 將運算式樹狀架構轉換回可執行檔程式碼。It also demonstrates how to turn the expression tree back into executable code by using the Compile method.


// Lambda expression as executable code.
Func<int, bool> deleg = i => i < 5;
// Invoke the delegate and display the output.
Console.WriteLine("deleg(4) = {0}", deleg(4));

// 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> deleg2 = expr.Compile();
// Invoke the method and print the output.
Console.WriteLine("deleg2(4) = {0}", deleg2(4));

/*  This code produces the following output:

    deleg(4) = True
    deleg2(4) = True
*/


' Lambda expression as executable code.
Dim deleg As Func(Of Integer, Boolean) = Function(ByVal i) i < 5
' Invoke the delegate and display the output.
MsgBox(String.Format("deleg(4) = {0}", deleg(4)))

' Lambda expression as data in the form of an expression tree.
Dim expr As System.Linq.Expressions.Expression(Of Func(Of Integer, Boolean)) = Function(ByVal i) i < 5
' Compile the expression tree into executable code.
Dim deleg2 As Func(Of Integer, Boolean) = expr.Compile()
' Invoke the method and print the output.
MsgBox(String.Format("deleg2(4) = {0}", deleg2(4)))

' This code produces the following output:
'
' deleg(4) = True
' deleg2(4) = True

備註

將 lambda 運算式指派給類型為Expression<TDelegate>的變數、欄位或參數時, 編譯器會發出指示來建立運算式樹狀架構。When a lambda expression is assigned to a variable, field, or parameter whose type is Expression<TDelegate>, the compiler emits instructions to build an expression tree.

注意

如果從 lambda 運算式轉換為類型的Expression<D>委派Expression(Of D) D , 則從 lambda 運算式轉換成類型 (在 Visual Basic 中) 會存在。A conversion from a lambda expression to type Expression<D> (Expression(Of D) in Visual Basic) exists if a conversion from the lambda expression to a delegate of type D exists. 不過, 轉換可能會失敗, 例如, 如果 lambda 運算式的主體是區塊。However, the conversion may fail, for example, if the body of the lambda expression is a block. 這表示委派和運算式樹狀結構的行為類似于多載解析。This means that delegates and expression trees behave similarly with regard to overload resolution.

運算式樹狀架構是 lambda 運算式的記憶體內部資料標記法。The expression tree is an in-memory data representation of the lambda expression. 運算式樹狀架構會使 lambda 運算式的結構變成透明和明確。The expression tree makes the structure of the lambda expression transparent and explicit. 您可以像使用任何其他資料結構一樣, 與運算式樹狀架構中的資料互動。You can interact with the data in the expression tree just as you can with any other data structure.

將運算式視為資料結構的能力, 可讓 Api 以自訂方式檢查、轉換和處理的格式接收使用者程式碼。The ability to treat expressions as data structures enables APIs to receive user code in a format that can be inspected, transformed, and processed in a custom manner. 例如, LINQ to SQLLINQ to SQL資料存取執行會使用這項功能, 將運算式樹狀架構轉譯成可由資料庫評估的 transact-sql 語句。For example, the LINQ to SQLLINQ to SQL data access implementation uses this facility to translate expression trees to Transact-SQL statements that can be evaluated by the database.

Queryable類別中定義的許多標準查詢運算子都有一或多個Expression<TDelegate>類型的參數。Many standard query operators defined in the Queryable class have one or more parameters of type Expression<TDelegate>.

NodeType的為。Expression<TDelegate> LambdaThe NodeType of an Expression<TDelegate> is Lambda.

Lambda<TDelegate>(Expression, IEnumerable<ParameterExpression>)使用或Lambda<TDelegate>(Expression, ParameterExpression[])方法來建立Expression<TDelegate>物件。Use the Lambda<TDelegate>(Expression, IEnumerable<ParameterExpression>) or Lambda<TDelegate>(Expression, ParameterExpression[]) method to create an Expression<TDelegate> object.

方法

Accept(ExpressionVisitor) Accept(ExpressionVisitor) Accept(ExpressionVisitor) Accept(ExpressionVisitor)

分派給這個節點類型的特定造訪方法。Dispatches to the specific visit method for this node type. 例如,MethodCallExpression 會呼叫 VisitMethodCall(MethodCallExpression)For example, MethodCallExpression calls the VisitMethodCall(MethodCallExpression).

(Inherited from Expression)
Compile() Compile() Compile() Compile()

將運算式樹狀架構所描述的 Lambda 運算式編譯為可執行程式碼,並產生表示 Lambda 運算式的委派。Compiles the lambda expression described by the expression tree into executable code and produces a delegate that represents the lambda expression.

Compile(Boolean) Compile(Boolean) Compile(Boolean) Compile(Boolean)

將運算式樹狀架構所描述的 Lambda 運算式,編譯成已解譯或已編譯的程式碼,並產生代表 Lambda 運算式的委派。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) Compile(DebugInfoGenerator) Compile(DebugInfoGenerator) Compile(DebugInfoGenerator)

產生代表 Lambda 運算式的委派。Produces a delegate that represents the lambda expression.

CompileToMethod(MethodBuilder) CompileToMethod(MethodBuilder) CompileToMethod(MethodBuilder) CompileToMethod(MethodBuilder)

將 Lambda 編譯為方法定義。Compiles the lambda into a method definition.

(Inherited from LambdaExpression)
CompileToMethod(MethodBuilder, DebugInfoGenerator) CompileToMethod(MethodBuilder, DebugInfoGenerator) CompileToMethod(MethodBuilder, DebugInfoGenerator) CompileToMethod(MethodBuilder, DebugInfoGenerator)

將 Lambda 編譯為方法定義和自訂偵錯資訊。Compiles the lambda into a method definition and custom debug information.

(Inherited from LambdaExpression)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Reduce() Reduce() Reduce() Reduce()

將這個節點精簡為更簡單的運算式。Reduces this node to a simpler expression. 如果 CanReduce 傳回 true,則應該傳回有效的運算式。If CanReduce returns true, this should return a valid expression. 這個方法可以傳回其他本身必須精簡的節點。This method can return another node which itself must be reduced.

(Inherited from Expression)
ReduceAndCheck() ReduceAndCheck() ReduceAndCheck() ReduceAndCheck()

將這個節點精簡為更簡單的運算式。Reduces this node to a simpler expression. 如果 CanReduce 傳回 true,則應該傳回有效的運算式。If CanReduce returns true, this should return a valid expression. 這個方法可以傳回其他本身必須精簡的節點。This method can return another node which itself must be reduced.

(Inherited from Expression)
ReduceExtensions() ReduceExtensions() ReduceExtensions() ReduceExtensions()

將運算式精簡為已知的節點類型 (不是 Extension 節點),如果已經是已知的類型,則僅傳回運算式。Reduces the expression to a known node type (that is not an Extension node) or just returns the expression if it is already a known type.

(Inherited from Expression)
ToString() ToString() ToString() ToString()

傳回 Expression 的文字表示。Returns a textual representation of the Expression.

(Inherited from Expression)
Update(Expression, IEnumerable<ParameterExpression>) Update(Expression, IEnumerable<ParameterExpression>) Update(Expression, IEnumerable<ParameterExpression>) Update(Expression, IEnumerable<ParameterExpression>)

建立與這個項目類似的新運算式,但是使用提供的子系。Creates a new expression that is like this one, but using the supplied children. 如果所有子系都相同,它將會傳回這個運算式。If all of the children are the same, it will return this expression.

VisitChildren(ExpressionVisitor) VisitChildren(ExpressionVisitor) VisitChildren(ExpressionVisitor) VisitChildren(ExpressionVisitor)

精簡節點,然後呼叫精簡後的運算式上的訪問項委派。Reduces the node and then calls the visitor delegate on the reduced expression. 如果節點無法縮減,此方法會擲回例外狀況。The method throws an exception if the node is not reducible.

(Inherited from Expression)

屬性

Body Body Body Body

取得 Lambda 運算式的主體。Gets the body of the lambda expression.

(Inherited from LambdaExpression)
CanReduce CanReduce CanReduce CanReduce

表示節點可精簡為更簡單的節點。Indicates that the node can be reduced to a simpler node. 如果傳回 true,則可呼叫 Reduce() 以產生精簡的形式。If this returns true, Reduce() can be called to produce the reduced form.

(Inherited from Expression)
Name Name Name Name

取得 Lambda 運算式的名稱。Gets the name of the lambda expression.

(Inherited from LambdaExpression)
NodeType NodeType NodeType NodeType

傳回這個 Expression 的節點型別。Returns the node type of this Expression.

(Inherited from LambdaExpression)
Parameters Parameters Parameters Parameters

取得 Lambda 運算式的參數。Gets the parameters of the lambda expression.

(Inherited from LambdaExpression)
ReturnType ReturnType ReturnType ReturnType

取得 Lambda 運算式的傳回型別。Gets the return type of the lambda expression.

(Inherited from LambdaExpression)
TailCall TailCall TailCall TailCall

取得值,這個值表示 Lambda 運算式是否會以 tail 呼叫最佳化來編譯。Gets the value that indicates if the lambda expression will be compiled with the tail call optimization.

(Inherited from LambdaExpression)
Type Type Type Type

取得此 Expression 代表之運算式的靜態類型。Gets the static type of the expression that this Expression represents.

(Inherited from LambdaExpression)

適用於

另請參閱