View Assembly Contents in C# and Usage of SuppressIldasmAttribute


As we know .Net converted into MSIL (Microsoft Intermediate Language) code after compilation and stored in .dll or .exe files. Sometimes we may want to check the dll code deployed in production to solve some issue. In this type of scenarios ILDASM.exe is very helpful to view Microsoft Intermediate Language code. In this article we discuss about how to view assembly or dll code by using ILDASM and also advantages of SuppressIldasmAttribute.

ILDASM.exe is part of Microsoft Visual Studio run time, you can open ildasm by using visual studio command prompt. We will examine this with simple example.

Open Microsoft Visual Studio 2015 => Select New Project => Select Class Library from templates and name it as CSharpILDASM. Rename class to MathClass and add below code.

namespace CSharpILDASM

{

    public class MathClass

    {

        public int Add(int a, int b)

        {

            return a + b;

        }

 

        public int Sub(int a, int b)

        {

            return a - b;

        }

    }

}

 

Now build the code to generate MSIL code that means it generates dll file. Now open Microsoft Visual Studio Command Prompt and enter ildasm as shown below.

It opens idasm window as shown below.

Go to File => select Open => open CSharpILDASM.dll file which was generate earlier as shown below.

It displays assembly information which includes Add() and Sub() methods as shown below.

By using ildasm.exe we can view public properties and variables also.

 

SuppressIldasmAttribute

Sometimes we may want to restrict to view our assembly code from others. For example if you are providing the dlls to your client, we don’t want our client to see the code. We can achieve this by applying SuppressIldasmAttribute attribute as shown below.

using System;

using System.Runtime.CompilerServices;

 

[assembly: SuppressIldasmAttribute]

namespace CSharpILDASM

{  

    public class MathClass

    {

        public int Add(int a, int b)

        {

            return a + b;

        }

 

        public int Sub(int a, int b)

        {

            return a - b;

        }

    }

}

 

We can’t see the MSIL code of above and we will get below error when we try to see the assembly code by using ildasm.exe.