作为一个新奇事物,我试图看看 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);
不幸的是,这会引发异常,因为 GetMethodBody 显然是对表达式树生成的代码的非法操作。如何以库的方式(即不使用外部工具,除非该工具具有 API)查看使用轻量级代码生成的代码生成的代码?
编辑:错误发生在第 5 行,compiled.Method.GetMethodBody() 抛出异常。
Edit2:有谁知道如何恢复方法中声明的局部变量?或者没有办法GetVariables?