GetFields 方法获取枚举值

发布于 2024-12-15 16:59:21 字数 260 浏览 1 评论 0原文

  1. 我注意到,在对枚举类型调用 GetFields() 时,我得到了一个 int32 类型的额外字段。它是从哪里来的?
  2. 当我调用另一个重载 (GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static) ) 时,它返回所需的字段。这是否意味着枚举的字段不是 Public ?

谢谢

  1. I have noticed that when calling GetFields() on enum type, I'm getting an extra fields with type int32. where did it come from??
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千紇 2024-12-22 16:59:21

Reflector IL Spy 可以解释这一点。

看一下反编译的枚举,您会看到如下所示的内容:

.class public auto ansi sealed ConsoleApplication1.Foo
    extends [mscorlib]System.Enum
{
    // Fields
    .field public specialname rtspecialname int32 value__
    .field public static literal valuetype ConsoleApplication1.Foo Bar = int32(0)
    .field public static literal valuetype ConsoleApplication1.Foo Baz = int32(1)

} // end of class ConsoleApplication1.Foo

Foo 枚举被实现为一个密封类,它包装了一个名为 value__ 的 int32 - 您看到的额外字段。

值得注意的是,它还继承自 System.Enum,其中还具有额外的(静态)字段。

Reflector IL Spy can explain this.

Take a look at a decompiled enum and you will see something that looks like this:

.class public auto ansi sealed ConsoleApplication1.Foo
    extends [mscorlib]System.Enum
{
    // Fields
    .field public specialname rtspecialname int32 value__
    .field public static literal valuetype ConsoleApplication1.Foo Bar = int32(0)
    .field public static literal valuetype ConsoleApplication1.Foo Baz = int32(1)

} // end of class ConsoleApplication1.Foo

i.e. the Foo enum is implemented as a sealed class that wraps an int32 called value__ - the extra field you are seeing.

Its worth noting that it also inherits from System.Enum which also has extra (static) fields.

虫児飞 2024-12-22 16:59:21

我怀疑该字段是基础值 - 毕竟,该值必须存储在某个地方。所以像这样的枚举:

public enum Foo
{
    Bar = 0,
    Baz = 1;
}

有点像这样:

public struct Foo
{
    public static readonly Bar = new Foo(0);
    public static readonly Baz = new Foo(1);

    private readonly int value;

    public Foo(int value)
    {
        this.value = value;
    }
}

I suspect the field is the underlying value - after all, that value has to be stored somewhere. So an enum like this:

public enum Foo
{
    Bar = 0,
    Baz = 1;
}

is a bit like this:

public struct Foo
{
    public static readonly Bar = new Foo(0);
    public static readonly Baz = new Foo(1);

    private readonly int value;

    public Foo(int value)
    {
        this.value = value;
    }
}
我是男神闪亮亮 2024-12-22 16:59:21

请参阅公共语言基础设施 (CLI) 标准、ECMA 标准 335 中的“程序集和范围界定”。我会提供更具体的位置,但具体细节似乎可能会发生变化。请访问 Ecma International 了解该标准。请参阅“CLS 规则 7”,内容如下:

枚举的基础类型应为内置 CLS 整数类型,字段名称应为“value__”,并且该字段应标记为 RTSpecialName。

那是领域,对吗?我不太明白,但至少它试图解释它是什么。这是标准所要求的。

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:

The underlying type of an enum shall be a built-in CLS integer type, the name of the field shall be "value__", and that field shall be marked RTSpecialName.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文