Directory Freebies VS CheatSheet Forum

RSS

Email

Translate

Home About Archive Privacy Contact Advertise Write for Dev102
Posted by Shahar Y on Oct 13th, 2008 | Filed under .Net, ASP.Net, C#, Misc., Utilities |

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:

exportreflector

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

outputdirectory

Here we go, the code generation begins:

exportprocess

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:

fileselector

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?

Tags: , , , , , ,

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


  1. Samin Said on Oct 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.

  2. ajeeshco Said on Oct 30, 2009 :

    cool… it did help me a lot..

Post a Comment

Write Article for Dev102

Write for Dev102!

We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution

Learn More