Viewing CIL code and Assembly Metadata

Abhisek
Posted by in .NET Framework category on for Beginner level | Views : 8457 red flag

This article will describe you about how to view the contents of an .NET assembly. You can investigate the CIL code and the meta information of the assembly to strengthen your programming skill in .NET.
Viewing CIL code and Assembly Meta-data

      If you want to gain mastery over every namespace in the .NET platform, then you need to investigate its contents deeply. A namespace is unique just due to its content type that are somehow sementically related.

      The Intermediate Language Deassembler utility(ildasm.exe) allows you to load up any .NET assembly and investigate its contents, including the associated manifest, CIL code, and type metadata. Using this utility you can know deeply about what an assembly actually contains. If you are using Visual Studio 2008, then the ildasm.exe should be installed under "C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\". If you can,t find it then just search your system for  a file named "ildasm.exe".

Before running this tool just create a simple C# console application which takes two integers and swap it. Just look the below program.
using System;

namespace Swapping
{
    class Swap
    {
        int a, b, c;
        public void Get()
        {  
            Console.WriteLine("Enter 2 no");
           this.a = Convert.ToInt32(Console.ReadLine());
            this.b = Convert.ToInt32(Console.ReadLine());
        }
        public void Show()
        {
           this.c = this.a;
            this.a = this.b;
            this.b = this.c;
            Console.WriteLine("After Swapping a={0} b={1}", this.a, this.b);
        }
    }
}

      Build the above program. Now run the ildasm.exe and proceed to File->Open menu and browse the assembly you want to explore. In our case it is the Swap.exe assembly we just created. The 'ildasm.exe' will display the structure of the assembly using a tree view format.

ildasm.exe showing the information of our Swap.exe:-


     In the above assembly you can see that our three integer variables(a, b, c) are declare as private int32 in the assembly. Like this you can investigate actually what happens when we compile an program written in .NET languages.
Viewing CIL code

        In addition to showing the namespaces, types and members contained in an assembly, 'ildasm' also displays the CIL code for a given member. Just double click the Main() method of  Swap class. A separate window containing the CIL code will appear.Similarly you can see the CIL code of other two methods Get() and Show(). Se the below image.

CIL code of Main():-


You can see here how the instances are created for a method.

CIL code of Get():-


In the CIL code of get you can see how the data we enter in ReadLine() method is converted into its specific formats and stored in the variable and how the methods actually work.

CIL code of Show():-


The CIL code of Show() displays how the swapping is done and value is passed from one variable to other.
Viewing Type Metadata

       If you want to see the meta-data for the currently loaded assembly then press Ctrl+M. It will show the meta-data in a separate window. Below figure shows the meta-data for Get() and Show() methods.



Viewing Assembly Metadata

       If you want to investigate the contents of the assembly's manifest, then double click the MANIFEST icon. The below picture shows the manifest of our application.



       As we see here the meta-data contains all the information about an assembly. So assemblies are fully self descriptive. Here we have created a simple console application. You can also create a window application and investigate how the different controls actually works.

       The 'ildasm' has also other features like it can write all the information to a text file and so on. Just go to the Help menu to discover all of its features. The 'ildasm' will help you to see and understand how your code is processed into the CIL code. Understanding the CIL code will strengthen your programming skills.Just investigate and expand your knowledge.

Page copy protected against web site content infringement by Copyscape

About the Author

Abhisek
Full Name: Abhisek Panda
Member Level: Bronze
Member Status: Member
Member Since: 10/11/2009 6:25:59 AM
Country: India
Abhisek Panda

Thanks and Regards Abhisek Panda Go Green And Save Your Future

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)