What is the max LINQ Expression Trees can do?

我的未来我决定 提交于 2019-12-19 04:01:00

问题


What is the maximum that LINQ expression Tree can do?

Can it define a class? How about a method, with all the declared name, modifiers, parametertype and return type?

Must the program always define the tree itself? Is it possible to generate the tree from a given C# file?


回答1:


In C# 3, expression trees can represent expressions. Hence the name. And they are further restricted to a subset of C# expressions -- no assignment expressions, no expressions involving pointer types, and so on.

In the libraries that will ship with C# 4, we have extended the expression tree library to also support statement trees. However, C# 4 will NOT automatically translate a statement-lambda into a "statement tree".

That is an obvious and useful feature which we simply did not have time to get to for C# 4. We'll consider it for hypothetical future versions. If you have a really great user scenario for statement trees, I'd love to hear it.

The obvious further extension to all that is declaration trees, which would represent class declarations, struct declarations, and so on. Having total homoiconicity between the C# language and the expression tree library would be awesome. It would enable all kinds of interesting metaprogramming scenarios. But that will not happen any time soon, so do not get your hopes up. That's more of a long-term dream of mine which might never happen.




回答2:


Expression trees:

http://www.interact-sw.co.uk/iangblog/2005/09/30/expressiontrees
http://www.davidhayden.com/blog/dave/archive/2006/12/18/ExpressionTrees.aspx
http://msdn.microsoft.com/en-us/library/bb397951.aspx
http://community.bartdesmet.net/blogs/bart/archive/2006/11/22/Getting-started-with-C_2300_-3.0-Expression-Trees.aspx
http://blah.winsmarts.com/2006/05/23/demystifying-c-30--part-7-expression-trees.aspx

Visitor pattern:

http://www.dofactory.com/Patterns/PatternVisitor.aspx
http://en.wikipedia.org/wiki/Visitor_pattern



来源:https://stackoverflow.com/questions/1430738/what-is-the-max-linq-expression-trees-can-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!