What is the max LINQ Expression Trees can do?
Asked Answered
P

2

3

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?

Principium answered 16/9, 2009 at 3:6 Comment(0)
S
15

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.

Sachi answered 16/9, 2009 at 15:18 Comment(6)
Thanks for ur reply. This is the library of Expression: msdn.microsoft.com/en-us/library/…. But I see MemberAssignment there, it is not for assignment expression? And this MethodCallExpression, shouldn't it be able to extract info from a method call (callee/caller/parameters)?Principium
No, that is not what MemberAssignment is for. I do not understand your second question.Sachi
Then what is MemberAssignment for? I mean that is it able to extract the details of an method invocation (callee, caller, parameter types)? Or is it even possible to define a method and call another method using Expression?Principium
MemberAssignment is for representing the notion "initialize the members of this newly-created object". It's how we represent initialization of an anonymous type instance in an expression tree. But rather than asking me and waiting for my response, why not just read the documentation that you linked to? That's probably a faster way to learn about expression trees.Sachi
Hi Eric, you said you'd love to hear if there were any great user scenarios for statement trees. I have one: #3989060 - just got pointed to this comment and thought you might like to know. Working around it with a delegate method for the moment but it doesn't read as clearly. Thanks!Eld
Eric it would massively increase the usability of the incredible powerful reflection.Emit library if we could use Expression trees to build types. I myself find that I have to build quite a large amount of helper methods for expressions to perform certain easy code patterns like foreach, using and for(;;) as these sugar patterns in C# are somewhat non-trivial to implement. It would be much easier to simple compose one foreach and visit and replace the foreach loop respectively.Earthshaking

© 2022 - 2024 — McMap. All rights reserved.