Intermediate Language (ILDASM & ILASM) Code in C#

Intermediate Language (ILDASM & ILASM) in C#.NET

In this article, I am going to discuss Intermediate Language (ILDASM & ILASM) Code in C# with Examples. Please read our previous article, where we discussed the .NET Program Execution Process Flow in detail. ILDASM stands for Intermediate Language disassembler and ILASM stands for Intermediate language assembler. As part of this article, we are going to discuss the following pointers and at the end of this article, you will understand all about Intermediate Language (IL Code) in C#.

  1. What happens when we compile a .NET Application?
  2. Understanding the Intermediate Language (IL Code) in C#?
  3. What are ILDASM and ILASM?
  4. How to view the Intermediate Language code in C#?
  5. What is Manifest?
  6. How to export the Intermediate Language code to a text file?
  7. How to rebuild an assembly from a text file which contains manifest and IL?
What happens when we compile a .NET Application?

When we compile any .NET application. it will generate an assembly with the extension of either a .DLL or an .EXE. For example, if you compile a Windows or Console application, then you will get an .EXE, whereas if you compile a Web or Class library project, then you will get a .DLL. Irrespective of whether it is a .DLL or .EXE, an assembly consists of two things i.e. Manifest and Intermediate language. Let us understand how the Intermediate Language and Manifest look like in .NET Framework with an example.

Understanding Intermediate Language (ILDASM and ILASM) Code in C#:

In order to understand Intermediate Language Code (ILDASM and ILASM) in C#, let us create a simple console application. Once you create the console application, please modify the Program class as shown below.

using System;
namespace ILDASMDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Understanding ILDASM and ILASM");
            Console.Read();
        }
    }
}

Now, build the application. Once you build the application, the above source code is compiled and intermediate language code generated and packaged into an assembly. In order to see the assembly, just right-click on the Project and select Open Folder in File Explorer option and then go to the bin => Debug folder and you should see an assembly with .exe extension as shown in the below image as it is a console application.

Intermediate Language (IL Code) in C#

How to view the Intermediate Language Code in .NET Framework?

The .NET framework provides a nice tool called ILDASM (Intermediate Language DisAssember) to view the code of the intermediate language in C#.NET. In order to use the ILDASM tool, you need to follow the below steps. Open Visual Studio Command Prompt in Administrator mode as shown in the below image.

How to view the Intermediate Language code in C#?

Once you open the visual studio command prompt in administrative mode, then type the “Ildasm.exe C:\YourDirectoryPath\YourAssembly.exe” command and press enter. Here, you need to provide the exe path where your exe is generated. My exe is generated in the path “D:\ILDASMDemo\ILDASMDemo\bin\Debug\ILDASMDemo.exe”, so I execute the following code in the command prompt:

How to use ILDASM in C#

Once you type the above command and press enter, it should open the following ILDASM window.

ILDASM in C#.NET

As you can see, the assembly is consists of two things (Manifest and Intermediate language). Let us first discuss the intermediate language code and then we will discuss what Manifest is. Now, let us expand the ILDASMDemo and compare it with our code. For better understanding, please have a look at the below image. There is a constructor in ILDASM and this is because by default the .NET Framework provides a default constructor when there is no constructor in your class. You can also the Main method in the intermediate language code/

IL code in .NET ILDASM

Now, double click on the Main method on the ILDASM window to see the intermediate language generated for the Main method as shown below.

Intermediate Language (ILDASM & ILASM) in C#.NET

What is Manifest?

Manifest contains metadata about the assembly like the name of the assembly, the version number of the assembly, culture, and strong name information as shown in the below image.

What is Manifest

Metadata also contains information about the referenced assemblies. Each reference includes the dependent assembly’s name, assembly metadata (version, culture, operating system, and so on), and public key, if the assembly is strongly named.

How to change Assembly info?

It is also possible to change or modify some of the information in the assembly manifest using attributes. For example, if you want to modify the version number, then you need to follow the below steps.

Open AssemblyInfo.cs class file which is present under the Properties folder as shown below. Every project in .NET has a properties folder.

How to change Assembly info in .NET?

In this file, you will find one attribute called the AssemblyVersion attribute, which is by default set to 1.0.0.0. Now, change this value to 2.0.0.0 as shown below.

Intermediate Language (IL Code) in C#.NET

Now, rebuild the solution. But before that close the ILDASM window otherwise you will get an error. Once you rebuild the solution then open the assembly using the same ILDASM.exe in the command prompt as shown below.

Intermediate Language (IL Code) in C#.NET

Once you execute the above command, it should open the assembly. At the bottom, you can find the updated version number of the assembly as expected as shown below.

How to rebuild an assembly from a text file which contains manifest and IL?

How to export the Intermediate Language code to a text file?

If you want to export or save the Intermediate Language code into a text file, then you need to follow the below steps.

Select File Menu Option from the ILDASM tool and then select Dump and you will see “Dump Options Window” and click on the OK button on the “Dump Options Window” as shown below.

How to export the Intermediate Language code to a text file?

Now you need to enter the file name as per your choice. I am entering the file name as MyFile and save it to the D: drive. Now navigate to D: drive in windows explorer and you should see MyFile.il Now, open MyFile.il with notepad and you should see assembly metadata and IL code.

How to rebuild an assembly from a text file which contains manifest and IL?

If you want to rebuild an assembly from IL code then you need to use a tool called ILASM.exe. So, let’s go and create an assembly from the file (MyFile.il) that we just save. In order to rebuild an assembly, please follow the below steps.

Type the following command in “Visual Studio Command Prompt” and press enter
         ILASM.exe D:\MyFile.il
Now navigate to D: drive in windows explorer and you should see MyFile.exe. So, in short, we use ILASM.exe (Intermediate Language Assembler) to reconstruct an assembly from a text file that contains manifest and IL.

In the next article, I am going to discuss CTS (Common Type System) in detail. Here, in this article, I try to explain Intermediate Language Code in C#. I hope you understand ILDASM and ILASM tools and enjoy this IL Code in the C#.NET article.

5 thoughts on “Intermediate Language (ILDASM & ILASM) Code in C#”

  1. blank
    Burhanuddin Vejalpurwala

    Hi!! I tried the above method and created the Assembly with .exe. However, when I tried to perform ILDASM.exe and tried to open the ILDASM window, it gives me an error : “It has no valid CLR header and cannot be disassembled”.
    If you can give the explanation on this it would be great.

  2. blank

    Hello, Thank you for this article. I tried to open the ILDASM window using the command prompt but it gave me this error “File “….” not found or not a PE file. Please assist

Leave a Reply

Your email address will not be published. Required fields are marked *