運算式樹狀架構摘要Expression Trees Summary

上一個主題 -- 轉譯運算式Previous -- Translating Expressions

在本系列中,您已了解如何使用「運算式樹狀架構」** 來建立動態程式,將程式碼解譯為資料,並建立以該程式碼為基礎的新功能。In this series, you've seen how you can use expression trees to create dynamic programs that interpret code as data and build new functionality based on that code.

您可以查看運算式樹狀架構,以了解演算法的意圖。You can examine expression trees to understand the intent of an algorithm. 不只是查看該程式碼。You can not only examine that code. 您可以建立表示原始程式碼修改版本的新運算式樹狀架構。You can build new expression trees that represent modified versions of the original code.

您也可以使用運算式樹狀架構來檢視演算法,並將該演算法轉譯至其他語言或環境。You can also use expression trees to look at an algorithm, and translate that algorithm into another language or environment.

限制Limitations

某些較新的 C# 語言項目無法正確轉譯為運算式樹狀架構。There are some newer C# language elements that don't translate well into expression trees. 運算式樹狀架構不能含有 await 運算式或 async Lambda 運算式。Expression trees cannot contain await expressions, or async lambda expressions. C# 6 版本中新增的許多功能,似乎與寫入運算式樹狀架構的功能不同。Many of the features added in the C# 6 release don't appear exactly as written in expression trees. 相反地,較新的功能會以舊版的對等語法顯示於運算式樹狀架構中。Instead, newer features will be exposed in expressions trees in the equivalent, earlier syntax. 這可能不如您認為的是項限制。This may not be as much of a limitation as you might think. 事實上,這表示當引進新的語言功能時,解譯運算式樹狀架構的程式碼仍可能以相同方式運作。In fact, it means that your code that interprets expression trees will likely still work the same when new language features are introduced.

即使有這些限制,運算式樹狀架構還是可讓您建立與解譯和修改程式碼 (以資料結構表示) 有關的動態演算法。Even with these limitations, expression trees do enable you to create dynamic algorithms that rely on interpreting and modifying code that is represented as a data structure. 這是功能強大的工具,而且是 .NET 生態系統的其中一項功能,可讓 Entity Framework 等豐富的程式庫完成其功能。It's a powerful tool, and it's one of the features of the .NET ecosystem that enables rich libraries such as Entity Framework to accomplish what they do.