Click here to monitor SSC

Bart

Software Engineer - Red Gate Software

.NET Reflector Support: Why doesn't the analyzer work for enum values?

Published Friday, October 02, 2009 4:20 PM

You may have noticed that the analyzer doesn't show anything useful for enum values. If you analyze the enum itself you can see, for example, where it is used (see figure 1), however, if you choose one of the values, all the nodes in the analyzer will be empty (see figure 2).

Analyzer showing usages of the System.Data.SqlDbType enum.

Figure 1. Analyzer showing usages of the System.Data.SqlDbType enum.

Analysis of the Decimal value of the System.Data.SqlDbType enum.

Figure 2. Result of analyzing the Decimal value of the System.Data.SqlDbType enum. Note that both the "Used By" and "Assigned By" nodes are empty.

This happens because the enum values themselves don't appear in the compiled IL, only the numeric values of the underlying type, by default System.Int32, to which they map.

For example, say you have this enum:

public enum CustomColor
{
    Red
= 0,
   
Purple = 1,
   
Orange = 2
}

And you use it in a switch statement like this:

switch ( myCustomColor )
{
   
case Red:
        ...
       
break;
   
case Purple:
        ...
       
break;
   
case Orange:
        ...
       
break;
   
default:
        ...
       
break;
}

When you compile this code the "Red", "Purple", and "Orange" values don't end up in the IL: just 0, 1, and 2. So, when you decompile with .NET Reflector you end up with:

switch ( myCustomColor )
{
   
case 0:
       
...
       
break;
   
case 1:
       
...
       
break;
   
case 2:
       
...
       
break;
   
default:
        ...
       
break;
}

It's then impossible to find all usages of, for example, "Orange", because the information is lost from the compiled code, and if we mapped to the numeric value, and then searched for uses of that. well, you can imagine how many false positives there might be.

There are a couple of things we could consider doing about this:

  1. Disable the Analyze option for enum values.
  2. Do some sort of inference based on the type of the value in the switch statement.

I think (1) sounds like a sensible short term, whilst (2) might be something we would consider introducing in a future version of .NET Reflector. Note though that there are circumstances where (2) might not work correctly, and so could result in spurious, or missing, results in the analyzer.

It's also worth pointing out that having the PDB files for the assembly won't make any difference since they do not store this information.

by Bart Read
Filed Under: , ,

Comments

 

Jason Haley said:

Interesting Finds: October 3, 2009
October 3, 2009 7:54 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.
<October 2009>
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
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...