運算式樹狀架構Expression Trees

如果您使用過 LINQ,您會有豐富的程式庫使用經驗,其中 Func 類型是 API 集合的一部分If you have used LINQ, you have experience with a rich library where the Func types are part of the API set. (如果您不熟悉 LINQ,您可能會想要閱讀linq 教學課程和此專案前面有關lambda 運算式的文章。 ) 運算式樹狀結構提供與函式的引數更豐富的互動。(If you are not familiar with LINQ, you probably want to read the LINQ tutorial and the article about lambda expressions before this one.) Expression Trees provide richer interaction with the arguments that are functions.

當您建立 LINQ 查詢時,通常會使用 Lambda 運算式來撰寫函式引數。You write function arguments, typically using Lambda Expressions, when you create LINQ queries. 在一般 LINQ 查詢中,這些函式引數會轉換成編譯器所建立的委派。In a typical LINQ query, those function arguments are transformed into a delegate the compiler creates.

當您想要有更豐富的互動時,您必須使用「運算式樹狀架構」**。When you want to have a richer interaction, you need to use Expression Trees. 運算式樹狀架構會以您可以查看、修改或執行的結構來表示程式碼。Expression Trees represent code as a structure that you can examine, modify, or execute. 這些工具可讓您在執行階段操作程式碼。These tools give you the power to manipulate code during run time. 您可以撰寫程式碼來查看執行中的演算法,或插入新功能。You can write code that examines running algorithms, or injects new capabilities. 在更進階的案例中,您可以修改執行中的演算法,甚至將 C# 運算式轉譯為其他格式,以便在其他環境中執行。In more advanced scenarios, you can modify running algorithms, and even translate C# expressions into another form for execution in another environment.

您可能已撰寫使用運算式樹狀架構的程式碼。You've likely already written code that uses Expression Trees. Entity Framework 的 LINQ API 接受運算式樹狀架構作為 LINQ 查詢運算式模式的引數。Entity Framework's LINQ APIs accept Expression Trees as the arguments for the LINQ Query Expression Pattern. 這可讓 Entity Framework 將以 C# 撰寫的查詢,轉譯為在資料庫引擎中執行的 SQL。That enables Entity Framework to translate the query you wrote in C# into SQL that executes in the database engine. 另一個範例是 Moq,這是 .NET 的熱門模擬架構。Another example is Moq, which is a popular mocking framework for .NET.

本教學課程的其餘章節會探索運算式樹狀架構為何、查看支援運算式樹狀架構的架構類別,並示範如何使用運算式樹狀架構。The remaining sections of this tutorial will explore what Expression Trees are, examine the framework classes that support expression trees, and show you how to work with expression trees. 您將了解如何讀取運算式樹狀架構、如何建立運算式樹狀架構、如何建立修改後的運算式樹狀架構,以及如何執行由運算式樹狀架構表示的程式碼。You'll learn how to read expression trees, how to create expression trees, how to create modified expression trees, and how to execute the code represented by expression trees. 閱讀完後,您將能夠使用這些結構來建立豐富彈性的演算法。After reading, you will be ready to use these structures to create rich adaptive algorithms.

  1. 說明運算式樹狀架構Expression Trees Explained

    了解「運算式樹狀架構」** 背後的結構和概念。Understand the structure and concepts behind Expression Trees.

  2. 支援運算式樹狀架構的架構類型Framework Types Supporting Expression Trees

    了解定義及操作運算式樹狀架構的結構和類別。Learn about the structures and classes that define and manipulate expression trees.

  3. 執行運算式Executing Expressions

    了解如何將以 Lambda 運算式表示的運算式樹狀架構轉換成委派,再執行所產生的委派。Learn how to convert an expression tree represented as a Lambda Expression into a delegate and execute the resulting delegate.

  4. 解譯運算式Interpreting Expressions

    了解如何周遊及查看「運算式樹狀架構」**,以了解運算式樹狀架構所表示的程式碼。Learn how to traverse and examine expression trees to understand what code the expression tree represents.

  5. 建置運算式Building Expressions

    了解如何建構運算式樹狀架構的節點,並建立運算式樹狀架構。Learn how to construct the nodes for an expression tree and build expression trees.

  6. 轉譯運算式Translating Expressions

    了解如何建立修改後的運算式樹狀架構複本,或將運算式樹狀架構轉譯為其他格式。Learn how to build a modified copy of an expression tree, or translate an expression tree into a different format.

  7. 總結Summing up

    檢閱運算式樹狀架構的相關資訊。Review the information on expression trees.