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 string8: get_Text() cil managed9: {10: .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )11: // Code size 11 (0xb)
12: .maxstack 113: .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.017: 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 void24: set_Text(string 'value') cil managed25: {26: .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )27: // Code size 8 (0x8)
28: .maxstack 829: 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
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’
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.
Now you can select ‘New Horizonrtal Tab Group’ option from Windows menu.
This will create two windows: one for C# or VB.Net code and the other for IL.
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 –
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