相关疑难解决方法(0)

如何从DynamicMethod获取IL bytearray?

作为一个新奇的东西,我试图看看IL与运行时生成的轻量级代码与VS编译器生成的代码有什么不同,因为我注意到VS代码往往会运行不同的性能配置文件管型.

所以我写了下面的代码::

Func<object,string> vs = x=>(string)x;
Expression<Func<object,string>> exp = x=>(string)x;
var compiled = exp.Compile(); 
Array.ForEach(vs.Method.GetMethodBody().GetILAsByteArray(),Console.WriteLine);
Array.ForEach(compiled.Method.GetMethodBody().GetILAsByteArray(),Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会引发异常,因为GetMethodBody显然是对表达式树生成的代码的非法操作.我怎样才能以库的方式(即不使用外部工具,除非工具有API)查看代码使用轻量级codegen生成的代码?

编辑:错误发生在第5行,编译.Method.GetMethodBody()抛出异常.

Edit2:有谁知道如何恢复方法中声明的局部变量?或者没有办法GetVariables?

c# dynamicmethod

9
推荐指数
1
解决办法
2658
查看次数

标签 统计

c# ×1

dynamicmethod ×1