Click here to monitor SSC
Av rating:
Total votes: 14
Total comments: 1


Bob Cramblitt
.NET Reflector Saved their Bacon: Chris Kapilla’s Story
24 February 2009

.NET Reflector is not just a handy adjunct to Visual Studio for developers to browse their assemblies and understand classes and methods; Sometimes it can have a vital role. Many developers have told us that, occasionally, it had 'saved their bacon'. We were intrigued and decided to find out more by offering prizes and cash for “.NET Reflector Saved My Bacon” stories.

Late last month, Chris Massey, a Simple-Talk writer and editor, put out a call for anyone with stories about how .NET Reflector saved their day. Simple-Talk received several responses. They tend to reflect the many stories that one comes across in blogs: Typically, the latest source of a VB or C# application gets lost, and .NET Reflector is used for its powers of decompiling the IL code of the assembly into the C# or VB code that could produce that IL. 

One story that we heard from a developer is typical of the disasters that can befall a software project. She is a software developer with 12 years of experience, the last eight almost exclusively using Microsoft technologies.  Most of her projects are web applications.  She didn’t want her name published because she was concerned that “it makes me sound like an idiot.”  The story she told us evoked only sympathy. You’d be surprised how many professional developers have had the experience when working long hours under pressure. Mistakes happen!

The episode occurred when she attempted to convert a Visual Studio 2003 project to VS 2005.  The conversion failed, mangling the source completely. She thought she had up-to-date backups, but didn’t.  And, she trusted the conversion wizard to back up the original files – it didn’t.  She was suddenly left without the source code, and very much wished she wasn’t!

After the usual waves of panic had subsided, she discovered .NET Reflector, and she used .NET Reflector to reverse-engineer the original ..DLL, and was able to recreate the source file, losing only the comments. She used Denis Bauer’s .FileDisassembler add-in  to  dump the decompiler output to The VB.NET source.

Bacon saved.  And except for a scare that she’ll never forget, no one was the wiser, except her.

The Kapilla story

Chris Kapilla’s story is from two years’ ago, and he remembers it as if it was yesterday.  Kapilla (www.kapilla.net) has been a developer for more than 20 years and an independent consultant since 1995.  He feels he was born again with the advent of the .NET Framework, and has been using it to develop applications for the desktop, server applets and websites in C# since 2001.

Here’s the episode in Chris’ words, with only minor edits. 


"In February 2007, I was looking for some additional consulting work.  I contacted a local organization (which shall remain nameless for soon-to-be-obvious reasons) to see if they had anything that I could help them with.  In 1999-2000 I had written a Microsoft Word VBA document management system for them that they retail to members of their organization. Upon completion, I had moved on to other things and lost contact with them."

Who moved my...application?

‘I wasn’t really
 expecting much’

"After talking with them I found out that they had utilized the services of another company to move their application to a Word COM Add-in, but the developer had dropped off the face of the earth.  Despite the company’s best efforts, they were not able to contact him.  Amazingly, they did not have any source code for their product, and it was showing serious signs of needing some updates.  Of course, they were completely unable to make any product changes."

"I didn't say anything in my initial meeting with the client, but I thought it might be possible to recover the source code for the product if it hadn't been obfuscated."

"I had heard that .NET Reflector was good at retrieving source code from a compiled assembly and thought it was worth a try, but I wasn't really expecting much."

Reflector to the rescue

"After opening the component .DLLs of the application and browsing to the method signatures in Reflector, I was delighted to see the beautifully formatted C# source code produced for each method.  Since there were more than a hundred methods in this one class, however, the process of extracting each method’s source code soon started to get tedious. "

"It was then that I discovered the Expand Methods links at the bottom of each class definition:..."

"Clicking on this presented me with a complete class definition in one fell swoop:"

"From this point, it was clear sailing to create a complete set of source code for the entire project. Of course getting a clean compile out of all this was a non-trivial task – in large part because of ambiguous references between Windows.Forms types and Office.Core types. Reflector's hyperlinks would tell me which type the reference was in each case, but the code needed to be tweaked in a number of places to get everything squared away."

"Still, extracting the source for all the ..DLL and .exe and making the adjustments necessary for a clean compile took only 14 hours." 

"Once I recovered the source, there was a lot of debugging to do.  The biggest stumbling block was that COM add-ins make use of an unmanaged “shim” .DLL moderates between the unmanaged Word application and the managed .DLL."

"Since the source code for the shim was not available, the only option was to create a new one from scratch.  Very fortunately, Microsoft provides a COM Shim Wizard that automates the creation of a C++ ATL project.  From that point it was only a matter of getting all the registry settings correct and I had a running application."

"The final part of the project was refactoring the original code into more manageable chunks, splitting out new classes as appropriate, and re-designing a number of the methods.  With this all completed, we were finally back to square one and could begin the work of fixing some bugs and adding some new enhancements, such as a new version of the product for Word 2007:"

Attaining god status 

"I was flabbergasted by what a complete job Reflector did of restoring the C# source code.  I thought I would have to do it in small bits and pieces but Reflector created beautifully formatted code that only required a minimum of tweaks over a few days in order to turn it into a polished set of source code files with more than 20,000 lines of code with five different modules.  I immediately attained god status with my client, although I did give the credit to Reflector."

.NET Reflector is free, and can be downloaded here: An up-to-date listing, and the location of the latest version,  of add-ins is always kept at CodeProject:. The Add-Ins are reviewed here. As simple guide to .NET Reflector is .NET Reflector: Soup to Nuts



This article has been viewed 4076 times.
Bob Cramblitt

Author profile: Bob Cramblitt

Bob Cramblitt, based in Cary, N.C., writes about issues that impact the day-to-day work of IT and database professionals. He can be reached at info@cramco.com.

Search for other articles by Bob Cramblitt

Rate this article:   Avg rating: from a total of 14 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Typo in the last box
Posted by: marc_scheuner (view profile)
Posted on: Monday, April 06, 2009 at 12:11 PM
Message: That list of .NET Reflector add-ins is obviously kept at CodePLEX - not CodeProject! :-)
Marc

 






recommended site pinvoke

PInvoke.net is a user-driven wiki which provides .NET developers with native method signatures, so they don't have to spend time writing them from scratch.




TortoiseSVN and Subversion Cookbook Part 3: In, Out, and Around
 Subversion doesn't have to be difficult, especially if you have Michael Sorens's guide at hand. After... Read more...

Feature Usage Reporting in Early Access Programs
 After doing Web development, you can get very used to the luxury of having basic information about your... Read more...

Feature Usage Reporting in Early Access Programs
 After doing Web development, you can get very used to the luxury of having basic information about your... Read more...

TLS/SSL and .NET Framework 4.0
 The Secure Socket Layer is now essential for the secure exchange of digital data, and is most generally... Read more...

SmartAssembly: Eating Our Own Dogfood
 Quite often at Red Gate, we are some of our own most enthusiastic software-users. SmartAssembly is a... Read more...

A Complete URL Rewriting Solution for ASP.NET 2.0
 Ever wondered whether it's possible to create neater URLS, free of bulky Query String parameters?... Read more...

Visual Studio Setup - projects and custom actions
 This article describes the kinds of custom actions that can be used in your Visual Studio setup project. Read more...

.NET Application Architecture: the Data Access Layer
 Find out how to design a robust data access layer for your .NET applications. Read more...

Web Parts in ASP.NET 2.0
 Most Web Parts implementations allow users to create a single portal page where they can personalize... Read more...

Configuring Forms Authentication in SharePoint 2007
 Damon Armstrong provides a step-by-step guide to the processes, quirks and pitfalls of setting up... Read more...

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk