Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tool to view the compiler generated code

Tags:

c#

Does anybody know tools to view the C# compiler generated code for delegate?

I want to verify the following

class X
{
     public event D Ev;
}

Which is compiled to :

class X
{
     private D __Ev;  // field to hold the delegate

     public event D Ev {
           add {
                lock(this) { __Ev = __Ev + value; }
           }

           remove {
                lock(this) { __Ev = __Ev – value; }
           }
     }
}
like image 500
Southsouth Avatar asked Feb 03 '10 02:02

Southsouth


2 Answers

Your question makes no sense, but you're probably looking for Reflector.

EDIT: Now, your question does make sense. You're still looking for Reflector.
However, you'll need to set the Optimization to None in options.

It reveals,

private EventHandler<MyEventArgs> MyEvent;
public event EventHandler<MyEventArgs> MyEvent
{
    [MethodImpl(MethodImplOptions.Synchronized)] add
    {
        this.MyEvent = (EventHandler<MyEventArgs>) Delegate.Combine(this.MyEvent, value);
        return;
    }
    [MethodImpl(MethodImplOptions.Synchronized)] remove
    {
        this.MyEvent = (EventHandler<MyEventArgs>) Delegate.Remove(this.MyEvent, value);
        return;
    }
}
like image 63
SLaks Avatar answered Sep 21 '22 15:09

SLaks


Reflector was bought by RedGate and is now a commercial product.

A free alternative is dotPeek:

https://www.jetbrains.com/decompiler/

It's a very capable decompiler with plenty of options and every bit as good as Reflector was at the time it was purchased. I haven't used it since so can't comment on what you get for your money.


Another tool is LINQPad:

http://www.linqpad.net/

It disassembles C# as you type (which is handy) and will show you the CIL. However it won't attempt to decompile that back into C# for you.

like image 39
Drew Noakes Avatar answered Sep 20 '22 15:09

Drew Noakes