Tarik
Tarik

Reputation: 81711

What is value__ defined in Enum in C#

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();
    }
  }
}

Upvotes: 13

Views: 2249

Answers (1)

Grant Winney
Grant Winney

Reputation: 66439

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

Upvotes: 13

Related Questions