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>>(
Expression.Add(a,b),
a,
b
);
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.
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.