扭转前pression< Func键< T,TResult>> .Compile()? [英] Reverse of Expression<Func<T,TResult>>.Compile()?

查看:182
本文介绍了扭转前pression< Func键< T,TResult>> .Compile()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我们可以:

Expression<Func<int, bool>> predicate = x => x > 5;
var result = Enumerable.Range(0,10).Where(predicate.Compile());

如何:

Func<int,bool> predicate = x => x > 5;
Expression<Func<int,bool>> exp = predicate.Decompile();

这就是我想要得到相应的防爆pression的 Func键。这可能吗?

That is, I want to get the corresponding Expression of the Func. Is it possible?

推荐答案

有没有神奇的反编译()代表的情况下,短的解构IL(也许Mono.Cecil能做到)。如果你想要一个EX pression树,你得的启动的和前pression树,所以有防爆pression&LT; Func键&LT; INT ,布尔&GT;&GT; througout

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 have Expression<Func<int, bool>> througout.

作为一个边缘的情况下,你可以得到的基本的距离的方法委托信息的委托的。方法(即 MethodInfo的)和 .TARGET (即为arg0 ) - 然而,对于涉及拉姆达大多数情况下,或匿名方法,这将指向上采集类的编译器生成的方法,所以不会真正帮助你多少。这是pretty的多局限于场景,如:

As an edge case, you can get basic method delegate information from the delegate's .Method (the MethodInfo) and .Target (the arg0) - 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:

Func<string,int> parse = int.Parse;

这篇关于扭转前pression&LT; Func键&LT; T,TResult&GT;&GT; .Compile()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆