Export (0) Print
Expand All

EnumBuilder Class

Describes and represents an enumeration type.

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)

[ClassInterfaceAttribute(ClassInterfaceType.None)] 
[ComVisibleAttribute(true)] 
public sealed class EnumBuilder : Type, _EnumBuilder
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class EnumBuilder extends Type implements _EnumBuilder
ClassInterfaceAttribute(ClassInterfaceType.None) 
ComVisibleAttribute(true) 
public final class EnumBuilder extends Type implements _EnumBuilder
Not applicable.

NoteNote:

In the .NET Framework versions 1.0 and 1.1, it is necessary to define enumerations using TypeBuilder because EnumBuilder emits enumerations whose elements are of type Int32 instead of the enumeration type. In the .NET Framework version 2.0, EnumBuilder emits enumerations whose elements have the correct type.

The following code example demonstrates the construction of an enumeration within a dynamic assembly, using EnumBuilder. The example defines an enumeration named Elevation, with an underlying type of Int32, and creates two elements: Low, with a value of 0, and High, with a value of 1. After the type has been created, the assembly is saved with the name TempAssembly.dll. You can use the MSIL Disassembler (Ildasm.exe) to examine the contents of this assembly.

NoteNote:

Prior to the .NET Framework version 2.0, this code example does not produce a correct enumeration.

using System;
using System.Reflection;
using System.Reflection.Emit;

class Example
{
    public static void Main()
    {
        // Get the current application domain for the current thread.
        AppDomain currentDomain = AppDomain.CurrentDomain;
      
        // Create a dynamic assembly in the current application domain, 
        // and allow it to be executed and saved to disk.
        AssemblyName aName = new AssemblyName("TempAssembly");
        AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
            aName, AssemblyBuilderAccess.RunAndSave);
      
        // Define a dynamic module in "TempAssembly" assembly. For a single-
        // module assembly, the module has the same name as the assembly.
        ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
      
        // Define a public enumeration with the name "Elevation" and an 
        // underlying type of Integer.
        EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int));
      
        // Define two members, "High" and "Low".
        eb.DefineLiteral("Low", 0);
        eb.DefineLiteral("High", 1);

        // Create the type and save the assembly.
        Type finished = eb.CreateType();
        ab.Save(aName.Name + ".dll");

        foreach( object o in Enum.GetValues(finished) )
        {
            Console.WriteLine("{0}.{1} = {2}", finished, o, ((int) o));
        }
    }
}

/* This code example produces the following output:

Elevation.Low = 0
Elevation.High = 1 
 */

public class Example
{
    public static void main(String[] args)
    {
        // Get the current application domain for the current thread.
        AppDomain currentDomain = AppDomain.get_CurrentDomain();
      
        // Create a dynamic assembly in the current application domain, 
        // and allow it to be executed and saved to disk.
        AssemblyName aName = new AssemblyName("TempAssembly");
        AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
            aName, AssemblyBuilderAccess.RunAndSave);
      
        // Define a dynamic module in "TempAssembly" assembly. For a single-
        // module assembly, the module has the same name as the assembly.
        ModuleBuilder mb = 
            ab.DefineDynamicModule(aName.get_Name(), aName.get_Name() + ".dll");
      
        // Define a public enumeration with the name "Elevation" and an 
        // underlying type of Integer.
        EnumBuilder eb = 
            mb.DefineEnum("Elevation", TypeAttributes.Public, int.class.ToType());
      
        // Define two members, "High" and "Low".
        eb.DefineLiteral("Low", (Int32) 0);
        eb.DefineLiteral("High", (Int32) 1);

        // Create the type and save the assembly.
        Type finished = eb.CreateType();
        ab.Save(aName.get_Name() + ".dll");

        Array values = Enum.GetValues(finished);
        for (int i = 0; i < values.get_Length(); i++)
        {
            Console.WriteLine("{0}.{1} = {2}", finished, 
                values.GetValue(i), (Int32)values.GetValue(i));
        }
    }
} 

/* This code example produces the following output:

Elevation.Low = 0
Elevation.High = 1 
 */

System.Object
   System.Reflection.MemberInfo
     System.Type
      System.Reflection.Emit.EnumBuilder

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft