De-compile dlls – How to check compiled C-sharp code inside any dll

Do you know that you can take the compiler generated dll files and actually re-produce the original source code from it? Yes you heard it right and the process is called decompilation. The process of decompilation requires some guess work and it is nearly accurate for c-sharp source code generation from the dlls.

The good thing is that the hard work has already been done by many great people. There are various free decompiler tools available online. In this post we will use a decompiler to regenerate the source code from the dll generated by .net compiler.

We will use JustDecompile for this purpose you can download it here. This tool has been provided by Telerik free to use as it is open source. It is lightweight and installs in a few seconds. Lets dive in.

Get the source code and compile

For this purpose i created a simple .net core project with only one file named Program.cs. You can use your own source code otherwise.

using System;

namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Automatic calculator");

            Add(2, 3);
        }

        private static void Add(int a, int b)
        {
            var sum = a + b;

            Console.WriteLine("The sum is: " + sum);
        }
    }
}

A simple program that adds two numbers. Compile the code. It will generate the dlls in the bin folder under the root folder of the project. The dll will be named with the name of the project in this case it will be called Calculator.dll

[amazon_auto_links id=”529″]

Open dll with JustDecompile

Open JustDecompile and click on the Open button > File(s). Select the dll that you want to decompile. In this case it is Calculator.dll.

Note: If it asks you for the runtime, just skip it.

Using the left panel, expand the items under the Calculator.dll item. The result should look like this

The first level displays the namespaces expanding a namespace shows the Classes under it. In this case Calculator > Program.

Select Program Class and on the right its content will be displayed.

Following is the decompiled code that I copied. As you can see its nearly matches the original code.

using System;

namespace Calculator
{
    internal class Program
    {
        public Program()
        {
        }

        private static void Add(int a, int b)
        {
            int num = a + b;
            Console.WriteLine(string.Concat("The sum is: ", num.ToString()));
        }

        private static void Main(string[] args)
        {
            Console.WriteLine("Automatic calculator");
            Program.Add(2, 3);
        }
    }
}

Its pretty straightforward, isn’t it? Now you can view code in other third party libraries, improve the code or fix the errors and re-use it.

Happy Coding!

Leave a Reply