Expression<TDelegate> 類別

定義

將強類型 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>

範例

下列程式碼範例示範如何將 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. 它也會示範如何使用方法,將運算式樹狀架構轉換回可執行檔程式碼 CompileIt 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.

注意

Expression<D> Expression(Of D) 如果從 lambda 運算式轉換為類型的委派,則從 lambda 運算式轉換為類型 (Visual Basic) 存在 DA 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 SQL 資料存取實行會使用這項功能,將運算式樹狀架構轉譯為可由資料庫評估的 Transact-sql 語句。For example, the LINQ 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.

屬性

Body

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

(繼承來源 LambdaExpression)
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.

(繼承來源 Expression)
Name

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

(繼承來源 LambdaExpression)
NodeType

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

(繼承來源 LambdaExpression)
Parameters

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

(繼承來源 LambdaExpression)
ReturnType

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

(繼承來源 LambdaExpression)
TailCall

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

(繼承來源 LambdaExpression)
Type

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

(繼承來源 LambdaExpression)

方法

Accept(ExpressionVisitor)

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

(繼承來源 Expression)
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)

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

會產生代表 Lambda 運算式的解譯或編譯委派。Produces an interpreted or compiled delegate that represents the lambda expression.

(繼承來源 LambdaExpression)
Compile(DebugInfoGenerator)

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

Compile(DebugInfoGenerator)

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

(繼承來源 LambdaExpression)
CompileToMethod(MethodBuilder)

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

(繼承來源 LambdaExpression)
CompileToMethod(MethodBuilder, DebugInfoGenerator)

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

(繼承來源 LambdaExpression)
Equals(Object)

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

(繼承來源 Object)
GetHashCode()

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

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
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.

(繼承來源 Expression)
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.

(繼承來源 Expression)
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.

(繼承來源 Expression)
ToString()

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

(繼承來源 Expression)
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)

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

(繼承來源 Expression)

適用於