If you are a .NET developer, you should probably know the .NET Reflector which is a great tool for viewing, navigating and searching through the class hierarchies of .NET assemblies (even if you don’t have the code for them). I am using this tool a lot but here is something I only recently discovered: one can export an assembly and let the Reflector generate its source code. The output of this process is a directory with a project file and all of the source files.

Lets deep dive into this process. The first thing to do is to drag the assembly you’re interested in into the Reflector. I want to show my case on the XHTML sitemap validation tool dll, which can be downloaded from our Freebies page. The next step is to right click on this assembly and choose the Export menu option:

Now, you shall provide the output directory where the generated code will reside, and click Start:

Here we go, the code generation begins:

During this process, the Reflector may need some user interventions to help it find some of the generated assembly dependencies. If it happens, the following screen will pop up:

The text box may be empty if the Reflector has no clue about the dependency file location, or it may contain some location which is suggested by the Reflector. Provide the correct location and press OK. When the code generation is completed, click the Close button and go to the output directory to see what you got.

Open the project file, let Visual Studio convert it and try to compile. Being honest, you will be lucky if the code is compiled at the first try because there are some issues with this Export process. As for what I know, there might be problems generating:

Properties with both getter and setter. Assume your property is called FileName, get_FileName might be generated instead.

Enums: you will get the enum value (int) instead of the enum itself.

There might be more problems, but you can easily fix all of the compilation errors by yourselves. Now, just for fun, try to do this whole process for the reflector.exe! Sadly, when looking at the generated code, you realize that the Reflector fails to generate its own source code… Or maybe its intended?

6 Responses to “Generate An Assembly Source Code Using The Reflector”

Samin

Said on
October 14, 2008 :

There is a reflector addon called “File disassembler” I think it generally does the same, but you can choose the output project type and the interface is a bit nicer. But the whole thing has major problems with compiler generated code (e.g identifiers with invalid characters) but for small assemblies it’s a very good feature.

Great post. I just stumbled upon your website and wanted to state that I have really appreciated searching your blog posts. Regardless I’ll be subscribing to your own nourish and that i we imagine you compose again shortly!

Hello, This article helped me a lot working with one project as customer lost source codes and i needed to modify 7 years old software. Main my problem was then using reflector i did not specified .net assembies just done [skip] on all dll files which reflector asked then i got decompiled properties like get_PropertyName set_PropertyName (e.g. ComboBox.get_Items().get_Count()). After this article i decided to try to specify these dll (like. System, System.Data, System.Drawing and etc…) and result was fine! Thanks a lot!