gpt4 book ai didi

c# - 什么是 C# 枚举中定义的 value__

转载 作者:太空狗 更新时间:2023-10-29 21:29:18 24 4
gpt4 key购买 nike

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

最佳答案

您可以找到更多 here .海报甚至有示例代码可以帮助您解决问题...只需插入 BindingFlags.Public | BindingFlags.Static 在 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

关于c# - 什么是 C# 枚举中定义的 value__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10081668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com