6

I'm working with an external library that has an enum. There are some members of this enum that, when you call ToString() on them, return the name of a different member of the enum.

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 enum members have the same numerical value, you can get behavior like this, but I know, from decompilation and checking the values at run-time, that each member in the enum has a unique value.

Here's a snippet of the enum's 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 just another one of those fun quirks of enums in .NET? If the latter, is there a workaround for it?

(For what it's worth, Tokens is part of of the Microsoft.SqlServer.Management.SqlParser.Parser namespace in .NET.)

15
  • What happens if you set a breakpoint, then evaluate the .ToString expressions in the Immediate window?
    – Dai
    May 8, 2014 at 19:39
  • Is that a complete copy paste? The extra , at the end does not look right to me
    – paparazzo
    May 8, 2014 at 19:43
  • 4
    There is no TOKEN_FROM in the enum list (code from snippet i know) and I get testing this in dotnetfiddle -> TOKEN_OUTER = TOKEN_OUTER
    – Kunukn
    May 8, 2014 at 19:50
  • 6
    I'm thinking you may be looking at two different versions of the assembly. Your code (at design time) may be referencing a newer version (since you're able to use TOKEN_FROM yet when you inspect the DLL with dotPeek, it's not there) but perhaps the assembly loaded at runtime is an older version with different underlying values thus mismatching the names. May 8, 2014 at 19:52
  • 1
    Very easy way to find out whether there are duplicates you can do: add a switch statement with Tokens, let VS generate switch cases for you. Then compile. If duplicates are there compiler won't let you to compile. May 8, 2014 at 19:55

2 Answers 2

5

You are looking at two different versions of the assembly.

Your code (at design/compile time) is referencing a newer version (since you're able to use TOKEN_FROM yet when you inspect the DLL with dotPeek, it's not there). However, the assembly loaded at runtime is an older version with different underlying values thus mismatching the names.

You'll have to investigate how it is you're referencing mismatched DLLs. It could be the installed framework on the executing machine, or perhaps you have projects in the same solution referencing different versions, or perhaps some other reason (it's not possible to determine from the information you have provided).

Once you resolve why you're referencing two different versions and unify it to a single assembly version, the Enum.ToString() result should be as you expect it.

1
  • 1
    As a quick note, with this particular DLL, the issue came from the fact that Visual Studio uses that DLL (for Database projects), but I was referencing an older version in my project's References.
    – nateirvin
    May 15, 2014 at 14:15
1

I suggest probe:

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

and so on

1
  • 1
    Please indent your code so it is formatted properly
    – rjp
    May 8, 2014 at 21:07

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.