带有 System.Reflection.Emit 的嵌套枚举
我想创建一个带有嵌套枚举的类。
public class Foo
{
public enum Views
{
}
}
但是 System.Reflection.Emit.TypeBuilder 类没有 DefineNestedEnum,只有 DefinedNestedType。 ModuleBuilder.DefineEnum 的存在让我可以创建一个枚举,但我找不到让它嵌套的方法。 我可以创建一个枚举而不伪造它(即使用 EnumBuilder)吗?
我将我的解决方案移至下面的答案。
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. ModuleBuilder.DefineEnum exists that let's me create an enum but I find no way to make it nested. Can I create an enum without faking it (i.e., using EnumBuilder)?
I moved my solution to an answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅本文末尾的示例< /a>,这正是你想要的。 (您使用 DefineNestedType 和正确的参数)
See the example at the end of this article, which does exactly what you want. (You use DefineNestedType with the right arguments)
将我的答案移至此处。
我唯一能想到的就是将嵌套类型定义为扩展 System.Enum 的密封类,并定义具有常量值的 public|static|literal 字段。 这本质上就是 C# 编译器根据我通过反汇编学到的知识所做的事情。 如果我这样做并引用程序集,Intellisense 会将其识别为枚举,并且功能就像枚举一样。
这正是 MSDN 显示 Jeremy 链接的方法。
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. This is essentially what the C# compiler is doing based on what I've learned by disassembling it. If I do this and reference the assembly Intellisense recognizes it as an enum and functions just like an enum.
This is exactly the method MSDN shows that Jeremy linked.