BK.
BK.

Reputation: 395

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 compiled to? Basicly seeing what happens behind the scene?

Upvotes: 2

Views: 566

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1499660

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.

Upvotes: 5

RameshVel
RameshVel

Reputation: 65857

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

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124622

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

Upvotes: 2

Related Questions