如何确定.Net中幕后发生的事情

BK.*_*BK. 2 compiler-construction .net-3.5

我可以使用什么工具或方法来查看像匿名方法或LINQ语句编译的代码?基本上看看幕后发生了什么?

Jon*_*eet 5

反射器是一种很好的方法.

转到View/Options/Optimization并选择"None",这样它就不会尝试找出C#最初的内容.

例如,Main这个小应用程序的方法:

using System;

class Test    
{
    static void Main()
    {
        string other = "hello";
        Foo (x => new { Before = other + x, After = x + other });
    }

    static void Foo<T>(Func<int, T> func) {}
}
Run Code Online (Sandbox Code Playgroud)

被反编译为:

private static void Main()
{
    <>c__DisplayClass1 class2;
    class2 = new <>c__DisplayClass1();
    class2.other = "hello";
    Foo(new Func<int, <>f__AnonymousType0<string, string>>(class2.<Main>b__0));
    return;
}
Run Code Online (Sandbox Code Playgroud)

然后你查看<>c__DisplayClass1<>f_AnonymousType0了解更多细节等