Does anyone know if it is possible to detect the use of an enum's "ToString()" method
The reason I ask is because we have started obfuscating our code using Dotfuscator, and of course because that renames the enums, it plays merry hell with any use of enum.ToString()! It'd be great if FXCopy could detect such usage.
Also, I guess we'd need to detect enum.Parse() and other reflection-style access.
Any help would be much appreciated!

Possible to detect use of enum.ToString()?
T_
You might think it would be a rare occurance, but two out of twelve applications had this problem - and finding it was a bit awkward!
The amount of time wasted for us makes a special in-house rule an attractive option for us!
(By the way, you can't assume that enum.Parse() would be ok, since different applications can end up writing the same value - "a" - for different enums, which makes parsing ambiguous.)
Chaitanya Vempati
Dave R - 140362
This of course broke the parsing done by the C++ program.
It was quite clear that was happening, because the unobfuscated version output the correct value and the obfuscated version - with no other changes - output "a", which was the name of the renamed enum value.
Here's a little test program that demonstrates the problem:
internal class Class1
{
enum TestEnum
{
TestEnumValue
}
[STAThread]
static void Main(string[] args)
{
Console.WriteLine( "Enum = " + TestEnum.TestEnumValue.ToString() );
}
}
The IL for the unobfuscated Main() looks like this, and prints "TestEnumValue":
The obfuscated version looks like this, and prints "a":
tmg2006
I used Michael's source code to solve the problem - thanks Michael!
I also had to add a rule to detect the boxing of enums, because you can pass an enum value to something like String.Format(), and Enum.ToString() will be called inside String.Format() in an undetectable way.
At the end of the day, it turned out that around 20% of our applications had issues with Enum.ToString(), mostly where some text for an exception message was being generated. So the rules were very useful for us!
Johan Levin141712
Let us know here if you have further questions.
Michael
Steve Kass
It's very easy to detect a call to Enum.ToString(). What exactly is the bad pattern you need to protect against, though I don't think I understand, exactly, since in most cases, a rename wouldn't be relevant in IL for an enum member (since the underlying literal itself is usually emitted to IL, not the named enum member).
using System;
public enum Test { value = 1}
public class TestClass
{
public static void Main() { Console.WriteLine(Test.value.ToString());}
}
The code above didn't cause a possible issue that I could see. Can you provide the pattern that does Or otherwise clear up my confusion
Michael Fanning
VSTS Development: Code Analysis
Nobody1234
Where:
1. The enum value was displayed in the user interface
2. An external process outside of the Framework parsed the enum (such as above)
The first situation should be avoided by using localized versions of the enum. The second situation I would class as rare and I'm wondering if it really quantifies a rule to be written for it. Especially considering that in most situations if the developeer uses Framework methods to parse the string (ie Enum.Parse) then no problems would be encountered.