GetFields 方法获取枚举值
- 我注意到,在对枚举类型调用
GetFields()
时,我得到了一个 int32 类型的额外字段。它是从哪里来的? - 当我调用另一个重载
(GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static) )
时,它返回所需的字段。这是否意味着枚举的字段不是 Public ?
谢谢
- I have noticed that when calling
GetFields()
on enum type, I'm getting an extra fields with type int32. where did it come from?? - When I call the other overload
(GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static) )
, it returns the desired fields. is that means that the enum's fields are not Public ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ReflectorIL Spy 可以解释这一点。看一下反编译的枚举,您会看到如下所示的内容:
即
Foo
枚举被实现为一个密封类,它包装了一个名为value__ 的
- 您看到的额外字段。int32
值得注意的是,它还继承自 System.Enum,其中还具有额外的(静态)字段。
ReflectorIL Spy can explain this.Take a look at a decompiled enum and you will see something that looks like this:
i.e. the
Foo
enum is implemented as a sealed class that wraps anint32
calledvalue__
- the extra field you are seeing.Its worth noting that it also inherits from
System.Enum
which also has extra (static) fields.我怀疑该字段是基础值 - 毕竟,该值必须存储在某个地方。所以像这样的枚举:
有点像这样:
I suspect the field is the underlying value - after all, that value has to be stored somewhere. So an enum like this:
is a bit like this:
请参阅公共语言基础设施 (CLI) 标准、ECMA 标准 335 中的“程序集和范围界定”。我会提供更具体的位置,但具体细节似乎可能会发生变化。请访问 Ecma International 了解该标准。请参阅“CLS 规则 7”,内容如下:
那是领域,对吗?我不太明白,但至少它试图解释它是什么。这是标准所要求的。
See "Assemblies and scoping" in the Common Language Infrastructure (CLI) standard, ECMA standard 335. I would provide a more specific location but the specifics seem to be subject to change. Go to Ecma International for the standard. See "CLS Rule 7" that says:
That is the field, correct? I don't thoroughly understand that but at least it tries to explain what it is. It is required by the standard.