views:

73

answers:

4

What tool or method can I use to see what code something like an anonymous method or LINQ statement gets compiled to? Basicly seeing what happens behind the scene?

+2  A: 

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

Ed Swangren
+1  A: 

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..

Cheers

Ramesh Vel

Ramesh Vel
+6  A: 

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.

Jon Skeet
A: 

Thx, really helpfull

BK