Decompile An Assembly In C#

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

We can decompile it to know the structure of the code or behaviour 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 accidently and only having assemblies but no source code. I helped him to decompile the code and somehow hosted the website built from decompiled source code.

If you are looking for some solution or searching internal behaviour of any .NET class many time 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 decompiler 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 market.

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

ILDASM

The IL Disassembler (Ildasm.exe) is a Microsoft product and 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 File menu, open – choose a .dll or .exe file which 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. Complete source code of console application is,

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int a = 10;  
  13.             Console.WriteLine(a.ToString());  
  14.             Console.WriteLine("This is source code before decompile....");  
  15.         }  
  16.     }  
  17. }  
Open Console application exe in dotPeek, right click on it and choose Export to Project.

dotPeek

Give complete path where you want to extract it.

path

Click on export. The following is the decompiled code:
  1. // Decompiled with JetBrains decompiler  
  2. // Type: ConsoleApplication1.Program  
  3. // Assembly: ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null  
  4. // MVID: E2CA63A0-82BB-4E27-B72C-2F21C6F75931  
  5. // Assembly location: D:\DotNetSchoolsBlogExamples\AdaptorDemo\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe  
  6. using System;  
  7. namespace ConsoleApplication1  
  8. {  
  9.     internal class Program  
  10.     {  
  11.         private static void Main(string[] args)  
  12.         {  
  13.             Console.WriteLine(10. ToString());  
  14.             Console.WriteLine("This is source code before decompile....");  
  15.         }  
  16.     }  
  17. }  

Up Next
    Ebook Download
    View all
    Learn
    View all