Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

See the example at the end of this article, which does exactly what you want. (You use DefineNestedType with the right arguments)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top