Intermediate Language in C#

Intermediate Language in C#

In this article, I am going to discuss Intermediate Language in C# with example. 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 Code in C#.

  1. What happens when we compile a .NET Application?
  2. Understanding the Intermediate Language 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 either a .DLL or an .EXE. For example, if you compile 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 (Manifest and Intermediate language). Let us understand how the intermediate language and Manifest look like with an example.

Understanding Intermediate Language in C#:

In order to understand Intermediate Language 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 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 below as it is a console application.

Understanding Intermediate Language in C#

How to view the Intermediate Language code in C#?

The .NET framework provides a nice tool called ILDASM (Intermediate Language DisAssember) to view the code of intermediate language. In order to use the ILDASM tool, you need to follow the below steps.

Open Visual Studio Command Prompt in Administrator mode as shown below.

How to view the Intermediate Language code in C#?

Once you open the visual studio command prompt, then type “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#

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.

Understanding Intermediate Language (IL) code in C#

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

Viewing IL Code using ILDASM

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 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.

Changing Assembly Information in C#

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.

Using ILDASM.exe in visual studio command prompt

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 in C#. I hope you understand ILDASM and ILASM tools and enjoy this article.

Leave a Reply

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