Since we can:
Expression<Func<int, bool>> predicate = x => x > 5;
var result = Enumerable.Range(0,10).Where(predicate.Compile());
How can I:
Func<int,bool> predicate = x => x > 5;
Expression<Func<int,bool>> exp = predicate.Decompile();
That is, I want to get the corresponding Expression of the Func. Is it possible?
There is no magic
Decompile()for a delegate instance, short of deconstructing the IL (perhaps with mono.cecil). If you want an expression tree, you’ll have to start with an expression tree, so haveExpression<Func<int, bool>>througout.As an edge case, you can get basic method delegate information from the delegate’s
.Method(theMethodInfo) and.Target(thearg0) – however, for most scenarios involving a lambda or anonymous method this will point at the compiler-generate method on the capture class, so won’t really help you much. It is pretty much limited to scenarios like: