Wednesday, June 23, 2010

ILViewer

                This is an Add-in to Visual Studio

Description

                This helps us to view the MSIL (Microsoft Intermediate Language) generated for C# or VB.Net code we write.

Expected Users

                Those who are interested in studying -

                     How compiler is handling your code?

              How new language  features are implemented?

Example

                What is the MSIL equivalent for ‘property’ in C# or VB.Net?

         When you write a property in C#, compiler is generating some methods corresponding to it. Eventhough .property is there, the get_property and set_property methods are the working methods

                ie,

     public string Text { get; set; } is –

  1: .property instance string Text()
  2:  {
  3:    .get instance string Game.Classes.Dragger::get_Text()
  4:    .set instance void Game.Classes.Dragger::set_Text(string)
  5:  }
  6: 
  7:  .method public hidebysig specialname instance string 
  8:          get_Text() cil managed
  9:  {
 10:    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) 
 11:    // Code size       11 (0xb)
 12:    .maxstack  1
 13:    .locals init (string V_0)
 14:    IL_0000:  ldarg.0
 15:    IL_0001:  ldfld      string Game.Classes.Dragger::'<Text>k__BackingField'
 16:    IL_0006:  stloc.0
 17:    IL_0007:  br.s       IL_0009 
 18: 
 19:    IL_0009:  ldloc.0
 20:    IL_000a:  ret
 21:  } // end of method Dragger::get_Text 
 22: 
 23:  .method public hidebysig specialname instance void 
 24:          set_Text(string 'value') cil managed
 25:  {
 26:    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) 
 27:    // Code size       8 (0x8)
 28:    .maxstack  8
 29:    IL_0000:  ldarg.0
 30:    IL_0001:  ldarg.1
 31:    IL_0002:  stfld      string Game.Classes.Dragger::'<Text>k__BackingField'
 32:    IL_0007:  ret
 33:  } // end of method Dragger::set_Text
 34: 

Ie, for set and get methods with set_propertyName and get_propertyName are generated.

When you say – Text=”Hello”, compiler is doing – set_Text(“Hello”)

Use

                 Download ILViewer from http://ilviewer.codeplex.com/releases/view/59437 and install it.

Now you will  be able to see this plug in Visual Studio ‘Tools’ menu

clip_image002

Provide a shortcut key for ILViewer as –

                Go to: Tools => Environment => Keyboard

                Select ILViewer and provide shortcut key to be used. I am using ‘Ctrl+I’

                clip_image004

 

Open a .cs or .vb file in Visual Studio and run the ILViewer (press your shortcut key for ILViewer).

Now you can see the IL generated for your code file as a new file opened in Visual Studio.

 

clip_image006

Now you can select ‘New Horizonrtal Tab Group’ option from Windows menu.

clip_image008

This will create two windows: one for C# or VB.Net code and the other for IL.

clip_image010

After making changes in C# code, click short key for ILViewer and see the MSIL getting changed in lower pane.

Note:

1.       Whenever ILViewer is running, the focus should be in .cs or .vb file. Else you will get an error message as follows –

clip_image002

2.         Make sure that your code file doesn’t have any partial class, whose partial definition is in some other file . This results in compilation error.

Happy Learning

3 comments:

  1. Good work dude :) Nice to see your posts coming out

    ReplyDelete
  2. Kewl...Release the rest of your tools, you created so far, to the public. Could be beneficial to many of us :)

    ReplyDelete
  3. Thank you Anoop and Hari.
    I will work on other tools and will publish to public.
    Thanks for your support

    ReplyDelete