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:
- Dotnet IL Editor (DILE)
- dotPeek
- NET CodeReflect
- RedGate Reflector
- telerik JustDecompile
- IL.View
- ILSpy
- 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:
Press enter it will open a 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.
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.
Right click on and choose – Go to Implementation (Ctrl+F12),
You can also extract the source code from it.
Let’s create a console application. Complete source code of 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 Console application exe in dotPeek, right click on it and choose
Export to Project.
Give complete path where you want to extract it.
Click on export. The following is the decompiled code:
-
-
-
-
-
- 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....");
- }
- }
- }