Take the 2-minute tour ×

I'm looking for the ability to convert entire methods into Expression trees. Writing it out would suck. :)

So (trivial example) given the following text:

public static int Add(int a, int b)
   return a + b;

I want to either get an in-memory object that represents this, or the following text:

ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
var expectedExpression = Expression.Lambda<Func<int, int, int>>(

Any ideas? Has anyone perhaps done something with Roslyn that can do this?

EDIT: Clarification: I want to suck in any C# method (for example, the one above) as text, and produce a resulting expression. Basically I'm looking to compile any given C# method into Expression trees.

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

Yes Roslyn can do, but Roslyn has its own expression tree (they are called syntax trees), Roslyn tools allow you to load and execute expressions or statements.

You will have to write your own syntax tree walker to convert Roslyn syntax tree to your expression tree, but everything may not fit correctly.

share|improve this answer
I figured Roslyn can do it. Do you know of any existing source that has done it already? I'm looking for something available. –  Shlomo Nov 15 '11 at 18:59
Roslyn is out for a month only, I think it will take sometime because people will write and experiment with it. But you might something on code plex for expression compiler using irony etc. I think in sometime people will post projects on code plex for Roslyn or you can start one and ask people to contribute and it will build from there on. –  Akash Kava Nov 15 '11 at 19:02
add comment

Why not:

Expression<Func<int,int,int>> expr = (a,b) => a + b;
share|improve this answer
I'll clarify my question. I want a function that can scan the text of the method above, and produce either the text to construct the Expression tree, or the Expression tree object itself. –  Shlomo Nov 15 '11 at 18:50
add comment

See billchi_ms answer at: http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/e6364fec-29c5-4f1d-95ce-796feb25a8a9

The short answer is that we may provide, or someone may write a Roslyn tree to ET v2, but Roslyn trees can represent the full languages of VB and C# while ETs v2 cannot (for example, type definitions or some ref-involved exprs).

share|improve this answer
add comment

Expression trees themselves are not generated at runtime from anything other than Expressions or lambdas (meaning your first addition statement cannot be retrieved from your executable as an expression tree). You can, however, use the code DOM on C# code (not the executable), and build a translator from the DOM to an expression tree.

share|improve this answer
Quite the reverse - expression trees are generated at execution time. When the compiler sees a lambda expression with a target type of an expression tree, it emits IL to build an expression tree at execution time. You can write C# code to build an expression tree too, as shown in the question. –  Jon Skeet Nov 15 '11 at 18:42
I apologize, as I may have misstated what I intended to convey. I do not exclude actual Expressions or lambdas, but just regular statements themselves, such as saying var a = 1 + 2;. Such statements are not in the form of expression trees at execution time. –  drwelden Nov 15 '11 at 18:44
But lambda expressions can be converted into expression trees such that the abstract syntax tree is represented by an expression tree at execution time. It's still not really clear what you mean - I suggest you edit your answer to clarify. –  Jon Skeet Nov 15 '11 at 18:45
I will. I'm not always perfect with words. –  drwelden Nov 15 '11 at 18:46
add comment

Your Answer


