什么是Lambda表达式编译()方法呢? [英] What does Lambda Expression Compile() method do?
问题描述
我想了解C#AST。我不知道,究竟是什么编译()
从这个例子的方法一样。
//一些代码跳过
表达式来; Func键<字符串,INT,INT,串>>数据= Expression.Lambda< Func键<字符串,INT,INT,串>>(
Expression.Call(S,typeof运算(字符串).GetMethod(串,新类型[] {typeof运算(INT) typeof运算(INT)}),A,b),
S,A,b
);
Func键<字符串,INT,INT,串>有趣= data.Compile();
为避免误解,我明白了 Expression.Lambda
和 Expression.Call
结构。我感兴趣的是编译()
方法。它是否以某种方式产生真正的MSIL?我可以看到MSIL?
我感兴趣的是
编译()
方法。它是否以某种方式产生真正的MSIL?
块引用>
是的。编译方法运行在拉姆达体块的访问者,并动态生成IL每个子表达式。
如果您有兴趣学习如何吐IL自己,看到的这个如何使用轻量级代码生成 Hello World的例子。 (我注意到,如果你是在不得不使用轻量级代码生成一个部分受信任的应用程序域的不幸位置,然后东西可以得到与限制跳过能见度世界有点不可思议,看到的如果您感兴趣的主题肖恩法卡斯的文章。)
我能看到MSIL?
块引用>
是的,但你需要一个特殊的可视化。展示台我用来调试
编译()
,而我是执行我的部分,可以在这里下载:
http://blogs.msdn.com/b/haibo_luo/archive/2005/ 10月25日/ 484861.aspx
I am trying to understand AST in C#. I wonder, what exactly
Compile()
method from this example does.// Some code skipped Expression<Func<string, int, int, string>> data = Expression.Lambda<Func<string, int, int, string>>( Expression.Call(s, typeof(string).GetMethod("Substring", new Type[] { typeof(int), typeof(int) }), a, b), s, a, b ); Func<string, int, int, string> fun = data.Compile();
To prevent misunderstandings, I understand the
Expression.Lambda
andExpression.Call
constructs. What interests me is theCompile()
method. Does it somehow produce real MSIL? Can I see the MSIL?解决方案What interests me is the
Compile()
method. Does it somehow produce real MSIL?Yes. The Compile method runs a visitor over the lambda body block and generates IL dynamically for each subexpression.
If you're interested in learning how to spit IL yourself, see this "Hello World" example of how to use Lightweight Codegen. (I note that if you are in the unfortunate position of having to use Lightweight Codegen in a partially trusted appdomain then things can get a bit weird in a world with Restricted Skip Visibility; see Shawn Farkas's article on the subject if that interests you.)
Can I see the MSIL?
Yes, but you need a special "visualizer". The visualizer I used to debug
Compile()
while I was implementing my portions of it can be downloaded here:http://blogs.msdn.com/b/haibo_luo/archive/2005/10/25/484861.aspx
这篇关于什么是Lambda表达式编译()方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!