什么是在C#的Enum中定义的value__ [英] What is value__ defined in Enum in C#

查看:67
本文介绍了什么是在C#的Enum中定义的value__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里可能有什么 value __

value__
MSN
ICQ
YahooChat
GoogleTalk

我运行的代码很简单:

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


推荐答案

您可以在此处找到更多信息。海报甚至包含示例代码,应该可以解决您的问题...只需插入 BindingFlags.Public |

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().


通过反射,我想我会占上风并控制我的枚举困境。不幸的是,在枚举类型上调用GetFields会在返回的列表中添加一个名为value__的额外条目。浏览完Enum的反编译之后,我发现value__只是枚举用来保存所选成员的值的特殊实例字段。我还注意到实际的枚举成员确实标记为静态。因此,要解决此问题,您所需要做的就是调用BindingFlags设置为仅检索公共静态字段

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

这篇关于什么是在C#的Enum中定义的value__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆