19.3 Viewing Assembly Contents with ILDasm

 <  Day Day Up  >  

You want to examine the metadata and intermediate language ( IL ) code in an assembly.


Technique

Type in the command ILDasm from the VS.NET command prompt. (Note that this command is case-insensitive; typing in ildasm works too. But we use the usual case convention for the name of the IL disassembler utility here.)

A new ILDasm window opens. Click on the File menu and navigate to the assembly you want to examine. To illustrate what happens, we created a small Windows Forms application called HelloWorldDlg . Other than the default code added by VS.NET, this sample contains a button with a click-event handler that displays a message box:

 
 private void btnHelloWorld_Click(object sender, System.EventArgs e) {     MessageBox.Show("Hello, World!", "Hello World Message Box"); } 

Now, running ILDasm.exe and opening the release build of the compiled .exe file from this project results in Figure 19.1, which lists the types and assembly attributes in the assembly in a treeview.

Figure 19.1. Viewing an assembly with ILDasm.

graphics/19fig01.gif

If you want to examine the IL code for a function, double-click on that function in the tree control. For example, Figure 19.2 shows what you get if you click on the Form1.btnHelloWorld_Click() method.

Figure 19.2. Viewing the code for a method with ILDasm.

graphics/19fig02.gif

The IL shown in Figure 19.2 is what the C# code for the method compiles to, in a textual format.

To examine the manifest, which includes details of any referenced assemblies and any assembly-level attributes, double-click on the MANIFEST node in ILDasm's main treeview, as shown in Figure 19.3.

Figure 19.3. Viewing an assembly manifest with ILDasm.

graphics/19fig03.gif

If, instead of using the ILDasm Windows Forms-based user interface, you prefer to store the textual representation of the IL code and metadata in a text file for later viewing or editing, you can use the following command-line syntax:

 
 ILDasm  AssemblyFile  /out:  OutputFile  

For example, the command you use for this project is

 
 ILDasm HelloWorldDlg.exe /out:HelloWorldDlg.il 

This command causes ILDasm to dump a complete textual representation of the IL code and metadata into a text file. Note that although this file is a plain-text file, it's customary to give it the extension .il .

In most cases, you need to use the VS.NET command prompt to run ILDasm.exe . On most installations of VS.NET, you cannot run ILDasm.exe from the default Windows command prompt because the appropriate symbols and paths aren't set up correctly. You can find the VS.NET command prompt in the Start menu, under Visual Studio.NET 2003, Visual Studio.NET (or for VS.NET 2002, under Visual Studio.NET, Visual Studio.NET Tools).

Comments

The purpose of ILDasm is to disassemble the code and metadata in an assembly into a textual form. The actual code and metadata is, of course, stored in a binary format, but ILDasm provides a convenient textual representation, with much the same relationship to IL as assembly language has to native executable code. In general, you will rarely need to disassemble assemblies that you have compiled. However, doing so can be useful to better understand how the C# code that you write gets translated into IL. It might assist in your understanding subtle nuances of C#, or it might help you to optimize code. In addition, if you lose the source code for some compiled project, it might be the only way available to understand how the code was supposed to work! To read the IL code, you need some familiarity with IL. The various IL commands are defined in Partition III of the European Computer Manufacturers Association (ECMA, an organization dedicated to promoting open standards in information technology) specification for the Common Language Runtime (CLR), which you can find in the Tool Developers Guide folder of the .NET Framework SDK. Alternatively, you can download it from http://msdn.microsoft.com/net/ecma.

If you want to examine the values of attributes, such as assembly informational attributes in the disassembled IL, then you need to look out for the word .custom , which indicates an attribute definition. Suppose you have this attribute in your source code:

 
 [assembly: AssemblyProduct("C# Cookbook Sample ")] 

ILDasm disassembles the attribute in the compiled assembly to the following:

 
 .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 12 43 23 20 43 6F 6F 6B 62 6F 6F 6B 20 53 61 6D 70 6C 65 00 00 )    // ...C# Cookbook Sample.. 

The binary representation of the text is a consequence of the fact that attributes can have any binary value, depending on what data type the constructor of each attribute expects; their values are not confined to text.

Although using ILDasm to disassemble your own code can be useful, you should bear in mind that license agreements might prohibit you from disassembling code that you have purchased from other organizations. Check individual license agreements before you proceed.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net