c# – Resolve Linq.Expressions.NewExpression?

c# – Resolve Linq.Expressions.NewExpression?

Answer was originally answered by @dasblinkenlight, I have no clue why he deleted it, it just worked. Anyway should he choose to undelete his answer and Ill delete mine and credit him!

Anyway for the reference:

The answer depends on whether or not the NewExpression in question
references any parameters, and if you have access to these parameter
expressions in case it does.

If NewExpression has no parameters, simply construct a lambda
expression from it, compile, and run:

NewExpression myNewExpression = ... // You have this
var instantiator = (Func<MyResultType>)Expression
    .Lambda<Func<MyResultType>>(myNewExpression)
    .Compile();
MyResultType res = instantiator();

c# – Resolve Linq.Expressions.NewExpression?

Leave a Reply

Your email address will not be published.