Click here to monitor SSC

Bart

Software Engineer - Red Gate Software

.NET Reflector Support: .NET Reflector doesn't decompile some methods correctly - why not?

Published Thursday, July 30, 2009 5:24 PM

There are various reasons why .NET Reflector might fail to decompile a particular method, generate source that does not match the original source code, or generate source code that will not compile without some modifications being made. I've listed some of these reasons here, so, in no particular order:

  • The code has been obfuscated. Generally if this is a matter of type/member renaming then .NET Reflector will decompile the code without complaining, but it may not be possible to compile the code if the renaming resulted in members of different types being assigned the same name, which is perfectly legal in IL, but not in C# or VB, or if the obfuscated member names include Unicode non-printable characters, which is again perfectly legal within IL. If control flow obfuscation has been applied then .NET Reflector may completely fail to decompile the method because the obfuscated IL will look nothing like the IL that is generated as output from any of the .NET framework compilers. This makes it impossible for .NET Reflector, in its current form, to infer the original source code.
  • The compiled IL could be decompiled to more than one source code construct with equivalent functionality. Different source code constructs, e.g., switch statements and if statements, can often be compiled to very similar IL. Under these circumstances it can be difficult for .NET Reflector to determine the original source construct, and so it may pick the wrong one during decompilation.
  • The IL may have been compiled from a declarative language, such as F#. In this case, if you try to decompile to an OO/procedural language, such as C# or VB, you're likely to run into problems because the compiled IL again won't look anything like that output by either of the C# or VB compilers.
  • You haven't selected the correct .NET Framework optimisation in View > Options. If you're decompiling code written with .NET 3.5 you should switch to the .NET 3.5 optimisation in the Disassembler pane in the Options dialog. All you need to do is select .NET 3.5 from the Optimization drop-down. By default, .NET Reflector tries to generate source code targetting .NET 2.0.
  • You're trying to decompile .NET 4.0 code. We haven't released a version of .NET Reflector that includes .NET 4.0 support yet so there are bound to be problems. We'll be adding some level of support for .NET 4.0 in the next major release of .NET Reflector, which ideally will be available in early 2010.
  • The method you're trying to decompile is an unmanaged method called using P/Invoke. Please see below.

On the last point, .NET Reflector cannot decompile unmanaged code (please see http://www.simple-talk.com/community/blogs/bart/archive/2009/07/31/74218.aspx), so any methods accessed via P/Invoke won't be decompiled. These methods are defined outside of your assembly in unmanaged code, and in some cases, in the framework class libraries, these methods are implemented by the CLR itself. See figures 1 - 4 for examples.

Decompiled P/Invoke signature (C#).

Figure 1. Typical P/Invoke signature in disassembler view showing DllImport attribute on method declaration describing library in which method is defined (C# output).

Decompiled P/Invoke signature (VB).

Figure 2. P/Invoke signature for same method shown in figure 1, but using VB output.

Decompiled P/Invoke signature for internal CLR method (C#).

Figure 3. P/Invoke signature for method implemented internally by the CLR (C# output). Note the use of the MethodImpl attribute with MethodImplOptions.InternalCall, which indicates that the method is implemented by the CLR. This cannot be used from user code.

Decompiled P/Invoke signature for internal CLR method (VB).

Figure 4. P/Invoke signature for same internal CLR method shown in figure 3, but using VB output.

If none of the above applies, then the last thing I'll say is that .NET Reflector's decompilation support isn't perfect. However, we are working to improve it, so please do tell us when things go wrong so we can look at fixing them.

Note that if .NET Reflector is unable to decompile the source you can still view the IL. Please see this post for instructions:

http://www.simple-talk.com/community/blogs/bart/archive/2009/07/31/74220.aspx

by Bart Read
Filed Under: , ,

Comments

 

Bart said:

I see this question come up from time to time in the .NET Reflector mailbox, so I'm going to make a stab...
August 19, 2009 6:48 AM
 

Bart said:

Yes, you can. There are two possible approaches, both of which utilise add-ins to .NET Reflector.  The...
October 2, 2009 9:18 AM
You need to sign in to comment on this blog

About Bart Read

Bart has done many things since he started work at Red Gate Software Ltd in August 2004, but nowadays he's (mainly) the product manager for the .NET Developer Tools. He still feels like this is a bit like admitting you were cheering for the Empire whilst watching Star Wars, but for now he's along for the ride. In a previous incarnation he was a project manager leading the .NET Reflector Pro, ANTS Memory Profiler 5, ANTS Performance Profiler 4 & 5, and SQL Prompt 3.0 - 3.6 projects. He still occasionally writes some code and, in the past, has touched the code for most of the Red Gate SQL developer tools... some of them still haven't recovered from the shock. He was born and grew up in Dorset, was educated in Nottingham and London, and likes music and real ale. His photo is extremely misleading.
<July 2009>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...