简体   繁体   English

与System.Reflection.Emit嵌套的枚举

[英]Nested enums with System.Reflection.Emit

I want to create a class with a nested enum. 我想创建一个带有嵌套枚举的类。

public class Foo
{
    public enum Views
    {
    }
}

However System.Reflection.Emit.TypeBuilder class has no DefineNestedEnum only DefinedNestedType. 但是,System.Reflection.Emit.TypeBuilder类仅具有DefinedNestedType,而没有DefineNestedEnum。 ModuleBuilder.DefineEnum exists that let's me create an enum but I find no way to make it nested. 存在ModuleBuilder.DefineEnum,让我创建一个枚举,但找不到任何嵌套的方法。 Can I create an enum without faking it (ie, using EnumBuilder)? 我可以不伪造一个枚举(即使用EnumBuilder)吗?


I moved my solution to an answer below. 我将解决方案移到下面的答案。

See the example at the end of this article , which does exactly what you want. 请参阅本文末尾的示例,该示例完全满足您的要求。 (You use DefineNestedType with the right arguments) (您可以使用带有正确参数的DefineNestedType)

Moving my answer I put in the question to here. 将我的答案移到这里。


Only thing I can think of is to define a nested type as sealed class that extends System.Enum and define public|static|literal fields with constant values. 我唯一能想到的就是将嵌套类型定义为扩展System.Enum的密封类,并使用常量值定义public | literal | literal字段。 This is essentially what the C# compiler is doing based on what I've learned by disassembling it. 本质上,这是C#编译器根据我通过拆解所学的知识所做的事情。 If I do this and reference the assembly Intellisense recognizes it as an enum and functions just like an enum. 如果我这样做并引用程序集,则Intellisense会将其识别为枚举,并像枚举一样起作用。


This is exactly the method MSDN shows that Jeremy linked. 这正是MSDN显示Jeremy链接的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM