Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is value__ defined in Enum in C#

Tags:

c#

.net

enums

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();
    }
  }
}
like image 724
Tarik Avatar asked Apr 10 '12 00:04

Tarik


1 Answers

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

like image 179
Grant Winney Avatar answered Nov 03 '22 00:11

Grant Winney