13

What value__ might be here?

value__
MSN
ICQ
YahooChat
GoogleTalk

The code I ran is simple:

namespace EnumReflection
{
    enum Messengers
    {
      MSN,
      ICQ,
      YahooChat,
      GoogleTalk
    }

  class Program
  {
    static void Main(string[] args)
    {
      FieldInfo[] fields = typeof(Messengers).GetFields();

      foreach (var field in fields)
      {
        Console.WriteLine(field.Name);
      }

      Console.ReadLine();
    }
  }
}
riQQ
  • 9,878
  • 7
  • 49
  • 66
Tarik
  • 79,711
  • 83
  • 236
  • 349
  • possible duplicate of [What is the purpose of the public "value__" field that I can see in Reflector against my enum?](http://stackoverflow.com/questions/5214031/what-is-the-purpose-of-the-public-value-field-that-i-can-see-in-reflector-ag) – Hans Passant Apr 10 '12 at 01:06

1 Answers1

13

You can find more here. The poster even has sample code that should get you around the problem... just insert BindingFlags.Public | BindingFlags.Static in between the parentheses of GetFields().

By using reflection, I figured I would gain the upper hand and take control of my enum woes. Unfortunately, calling GetFields on an enum type adds an extra entry named value__ to the returned list. After browsing through the decompilation of Enum, I found that value__ is just a special instance field used by the enum to hold the value of the selected member. I also noticed that the actual enum members are really marked as static. So, to get around this problem, all you need to do is call GetFields with the BindingFlags set to only retrieve the public, static fields

riQQ
  • 9,878
  • 7
  • 49
  • 66
Grant Winney
  • 65,241
  • 13
  • 115
  • 165