开发者

How to determine what happens behind the scene in .Net

What tool or method can I use to see what code something like an anonymous method or LINQ statement gets co开发者_Go百科mpiled to? Basicly seeing what happens behind the scene?


Reflector is an excellent way to do this.

Go to View / Options / Optimization and choose "None" so that it won't try to work out what the C# originally was.

For example, the Main method of this little app:

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) {}
}

is decompiled to:

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;
}

and then you look in <>c__DisplayClass1 and <>f_AnonymousType0 for more details etc.


You can use ildasm to view the MSIL output of the compiler.


There is one another way if you wanted to get inside and whats going on in .Net framework when we call the CLR methods.

If you are using VS 2008, you can even debug the .net framework source code. For this you need to install Microsoft source reference server hotfixes..

And Shawn Burke got an excellent post (step by step guide) to configure this thing..

Just give a try..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜