Decompile An Assembly In C#

Introduction

Sometimes we need to decompile the .dll & .exe files created in C# or VB.NET. There are multiple reasons why we need to compile the .NET .dll & .exe files.

We can decompile it to know the structure of the code or behavior of the class or interface used inside this assembly.

About 2 years ago, my friend came to me and told me that he created a website in ASP.NET with C# and hosted it on a server, but he had deleted the source code accidentally and only had assemblies but no source code. I helped him to decompile the code and somehow hosted the website built from a decompiled source code.

If you are looking for some solution or searching for internal behavior of any .NET class, many times people say that they decompiled the code and say their finding.

To decompile the.NET assembly, we need a decompiler, that’s all. There are a lot of decompilers available in the market.

Some popular decompilers are

  1. Dotnet IL Editor (DILE)
  2. dotPeek
  3. NET CodeReflect
  4. RedGate Reflector
  5. telerik JustDecompile
  6. IL.View
  7. ILSpy
  8. ILDasm

And many more decompilers are available in the market.

But I am going to explain only 2 decompilers ILDasm & dotpeek. Because both are very popular and widely used.

What is ILDASM?

The IL Disassembler (Ildasm.exe) is a Microsoft product and an in-built disassembler for .NET, which is automatically installed with Visual Studio.

Open the Developer Command Prompt for Visual Studio 2013 or 2015 & type ILDASM

ILDASM

Press enter it will open a new window.

new window

Go to the File menu, open – choose a .dll or .exe file that you want to open. It will load the dll as in the following image.

dll

You can expand and explore here for Namespaces, Classes, Interfaces, Value Classes, Enums, Methods, Static methods, Fields, Static fields, Events, Properties, etc.

You can find the complete list of symbols here.

Decompile a DLL using dotPeek

dotPeek is a free-of-charge .NET decompiler and Assembly Browser from JetBrains. Download and install the dotPeek from here.

Open the dotPeek and choose any assembly I have chosen System.Data.dll.

System.Data.dll

Right-click on and choose – Go to Implementation (Ctrl+F12),

Implementation

You can also extract the source code from it.

Let’s create a console application. The complete source code of the console application is.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
namespace ConsoleApplication1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            int a = 10;  
            Console.WriteLine(a.ToString());  
            Console.WriteLine("This is source code before decompile....");  
        }  
    }  
}  

Open the Console application exe in dotPeek, right-click on it, and choose Export to Project.

dotPeek

Give a complete path where you want to extract it.

path

Click on export. The following is the decompiled code.

// Decompiled with JetBrains decompiler  
// Type: ConsoleApplication1.Program  
// Assembly: ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null  
// MVID: E2CA63A0-82BB-4E27-B72C-2F21C6F75931  
// Assembly location: D:\DotNetSchoolsBlogExamples\AdaptorDemo\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe  
using System;  
namespace ConsoleApplication1  
{  
    internal class Program  
    {  
        private static void Main(string[] args)  
        {  
            Console.WriteLine(10. ToString());  
            Console.WriteLine("This is source code before decompile....");  
        }  
    }  
}  


Recommended Free Ebook
Similar Articles