posted on Monday, May 01, 2006 2:19 PM by jritmeijer

Recovering 'lost' .net source code


Please note that the names have been changed to protect the not so innocent. If you think this is about you then believe me, it is not. This blog post is completely fictional, I am not even a developer or have any knowledge about this topic.
 

 
Right, so I am working on a fun project, a blogging engine, for which someone has 'misplaced' the latest version of the source code. To make matters worse it is not clear which files are up to date and which files are old. Luckily the compiled DLLs containing the most up to date code are still available from a production server.
 
Let's not go into how or why this happened, especially considering the culprit is no longer around...surprise..surprise, but let's focus on how we can resolve the situation.
 
.NET assemblies (DLLs etc) can be disassembled into their original source code using external tools. Variable names and comments will be lost, but the syntax and structure of the code is the same. The best known tool to reverse engineer assemblies into C# classes is Lutz Roeder's .NET Reflector. Unfortunately this application only disassembles one method at a time, which makes it very time consuming to compare large parts of code.
 
Luckily a third party plug-in is available that enhances Reflector to disassemble entire Assemblies into the original code.
 
 
So, how are we going to merge the changes into the source code? Well, here is how:
  1. Compile the code that we DO have into assemblies
  2. Generate C# code from this compiled assembly using the above mentioned tools
  3. Generate C# code from the assemblies from the live server using the above mentioned tools
  4. Use your favourite DIFF tools (SourceSafe?) to compare the generated files
  5. Merge the changes into the latest source code
  6. Clean up the merged code and attempt to add some comments and sensible variable names.

 
Again, this post is not about you, it is about someone else who lost the source code, really.
 

Comments