Why doesn't Enum.ToString () return the correct enumeration name?

I work with an external enumeration library. There are some members of this enumeration that, when calling ToString() return the name of another member of the enumeration on them.

 Console.WriteLine("TOKEN_RIGHT = {0}", Tokens.TOKEN_RIGHT.ToString()); //prints TOKEN_OUTER Console.WriteLine("TOKEN_FROM = {0}", Tokens.TOKEN_FROM.ToString()); //prints TOKEN_FROM Console.WriteLine("TOKEN_OUTER = {0}", Tokens.TOKEN_OUTER.ToString()); //prints TOKEN_FULL 

I know that when two members of an enumeration have the same numerical value, you can get behavior like this, but I know that after decompiling and checking the values ​​at runtime, each member in the enumeration has a unique value.

Here is a snippet of an enum definition (generated by dotPeek ):

 public enum Tokens { TOKEN_OR = 134, TOKEN_AND = 135, TOKEN_NOT = 136, TOKEN_DOUBLECOLON = 137, TOKEN_ELSE = 138, TOKEN_WITH = 139, TOKEN_WITH_CHECK = 140, TOKEN_GRANT = 141, TOKEN_CREATE = 142, TOKEN_DENY = 143, TOKEN_DROP = 144, TOKEN_ADD = 145, TOKEN_SET = 146, TOKEN_REVOKE = 147, TOKEN_CROSS = 148, TOKEN_FULL = 149, TOKEN_INNER = 150, TOKEN_OUTER = 151, TOKEN_LEFT = 152, TOKEN_RIGHT = 153, TOKEN_UNION = 154, TOKEN_JOIN = 155, TOKEN_PIVOT = 156, TOKEN_UNPIVOT = 157, TOKEN_FROM = 242, } 

Why is this happening? Is there something I'm doing wrong, or is this another one of those fun enum enums in .NET? If the latter, is there a workaround for him?

(Why is it Tokens is part of the Microsoft.SqlServer.Management.SqlParser.Parser namespace in .NET.)

+6
source share
2 answers

You are viewing two different versions of the assembly.

Your code (during development / compilation) refers to a newer version (since you can use TOKEN_FROM , but when you check the DLL with dotPeek, it does not exist). However, the assembly loaded at runtime is an older version with different base values, which does not match the names.

You will need to investigate how you are referencing inappropriate DLLs. This may be the installed environment on the executing machine, or perhaps you have projects in one solution that link to different versions, or, possibly, to another reason (from the information that you provided) it is impossible to determine.

Once you decide why you are referencing two different versions and merging them into one version of the assembly, the result of Enum.ToString() should be what you expect.

+5
source

I suggest a probe:

 Console.WriteLine("TOKEN_RIGHT = {0}", Tokens.TOKEN_RIGHT.ToString("F")); 

etc.

+1
source

All Articles