Export (0) Print
Expand All

EnumBuilder Class

Describes and represents an enumeration type.

For a list of all members of this type, see EnumBuilder Members.

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

[Visual Basic]
NotInheritable Public Class EnumBuilder
   Inherits Type
[C#]
public sealed class EnumBuilder : Type
[C++]
public __gc __sealed class EnumBuilder : public Type
[JScript]
public class EnumBuilder extends Type

Thread Safety

Reflection Emit is thread-safe when using assemblies that were created with the AppDomain.DefineDynamicAssembly method with the Boolean parameter isSynchronized set to true.

Example

[Visual Basic, C#, C++] The following code sample demonstrates the construction of an enumeration within a dynamic assembly, using EnumBuilder.

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

Public Class MyEnumBuilderSample
   Private Shared myAssemblyBuilder As AssemblyBuilder
   Private Shared myModuleBuilder As ModuleBuilder
   Private Shared myEnumBuilder As EnumBuilder
   
   
   Public Shared Sub Main()
      Try
         CreateCallee(Thread.GetDomain(), AssemblyBuilderAccess.Save)
         Dim myTypeArray As Type() = myModuleBuilder.GetTypes()
         Dim myType As Type
         For Each myType In  myTypeArray
            Console.WriteLine("Enum Builder defined in the module builder is: " + myType.Name)
         Next myType
         
         Console.WriteLine("Enum TypeToken is :" + myEnumBuilder.TypeToken.ToString())
         Console.WriteLine("Enum UnderLyingField is :" + myEnumBuilder.UnderlyingField.ToString())
         Console.WriteLine("Enum UnderLyingSystemType is :" + myEnumBuilder.UnderlyingSystemType.ToString())
         Console.WriteLine("Enum GUID is :" + myEnumBuilder.GUID.ToString())
         myAssemblyBuilder.Save("EmittedAssembly.dll")
      Catch ex As NotSupportedException
         Console.WriteLine("The following is the exception is raised: " + ex.Message)
      Catch e As Exception
         Console.WriteLine("The following is the exception raised: " + e.Message)
      End Try
   End Sub 'Main
   
   
   Private Shared Sub CreateCallee(myAppDomain As AppDomain, access As AssemblyBuilderAccess)
      ' Create a name for the assembly.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "EmittedAssembly"
      
      ' Create the dynamic assembly.
      myAssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAssemblyName, _
                 AssemblyBuilderAccess.Save)
      ' Create a dynamic module.
      myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("EmittedModule", "EmittedModule.mod")
      ' Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum", _
                 TypeAttributes.Public, GetType(Int32))
      
      Dim myFieldBuilder1 As FieldBuilder = myEnumBuilder.DefineLiteral("FieldOne", 1)
      Dim myFieldBuilder2 As FieldBuilder = myEnumBuilder.DefineLiteral("FieldTwo", 2)
      
      myEnumBuilder.CreateType()
   End Sub 'CreateCallee
End Class 'MyEnumBuilderSample

[C#] 

using System;
using System.Collections;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

public class MyEnumBuilderSample
{
   static AssemblyBuilder myAssemblyBuilder;
   static ModuleBuilder myModuleBuilder;
   static EnumBuilder myEnumBuilder;
   
   public static void Main()
   {
      try
      {
         CreateCallee(Thread.GetDomain(), AssemblyBuilderAccess.Save);
         Type[] myTypeArray = myModuleBuilder.GetTypes();
         foreach(Type myType in myTypeArray)
         {
            Console.WriteLine("Enum Builder defined in the module builder is: " 
               + myType.Name);
         }

         Console.WriteLine("Enum TypeToken is :" +  
                                       myEnumBuilder.TypeToken.ToString());
         Console.WriteLine("Enum UnderLyingField is :" +  
                                    myEnumBuilder.UnderlyingField.ToString());
         Console.WriteLine("Enum UnderLyingSystemType is :" +  
                              myEnumBuilder.UnderlyingSystemType.ToString());
         Console.WriteLine("Enum GUID is :" + myEnumBuilder.GUID.ToString());
         myAssemblyBuilder.Save("EmittedAssembly.dll");
      }
      catch(NotSupportedException ex)
      {
         Console.WriteLine("The following is the exception is raised: " + ex.Message);
      }
      catch(Exception e)
      {
         Console.WriteLine("The following is the exception raised: " + e.Message);
      }
   }

   private static void CreateCallee(AppDomain myAppDomain, AssemblyBuilderAccess access)
   {
      // Create a name for the assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";

      // Create the dynamic assembly.
      myAssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAssemblyName, 
                                             AssemblyBuilderAccess.Save);
      // Create a dynamic module.
      myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("EmittedModule", 
                                                         "EmittedModule.mod");
      // Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum", 
                                 TypeAttributes.Public, typeof(Int32));

      FieldBuilder myFieldBuilder1 = myEnumBuilder.DefineLiteral("FieldOne", 1);
      FieldBuilder myFieldBuilder2 = myEnumBuilder.DefineLiteral("FieldTwo", 2);

      myEnumBuilder.CreateType();
   }
}

[C++] 

#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

public __gc class MyEnumBuilderSample
{
   static AssemblyBuilder* myAssemblyBuilder;
   static ModuleBuilder* myModuleBuilder;
   static EnumBuilder* myEnumBuilder;

public:   
   static void Main()
   {
      try
      {
         CreateCallee(Thread::GetDomain(), AssemblyBuilderAccess::Save);
         Type* myTypeArray[] = myModuleBuilder->GetTypes();
         IEnumerator* myEnum = myTypeArray->GetEnumerator();
         while (myEnum->MoveNext())
         {
             Type* myType = __try_cast<Type*>(myEnum->Current);
            Console::WriteLine(S"Enum Builder defined in the module builder is: {0}", myType->Name);
         }

         Console::WriteLine(S"Enum TypeToken is :{0}", __box(myEnumBuilder->TypeToken));
         Console::WriteLine(S"Enum UnderLyingField is :{0}", myEnumBuilder->UnderlyingField);
         Console::WriteLine(S"Enum UnderLyingSystemType is :{0}", myEnumBuilder->UnderlyingSystemType);
         Console::WriteLine(S"Enum GUID is :{0}", __box(myEnumBuilder->GUID));
         myAssemblyBuilder->Save(S"EmittedAssembly.dll");
      }
      catch(NotSupportedException* ex)
      {
         Console::WriteLine(S"The following is the exception is raised: {0}", ex->Message);
      }
      catch(Exception* e)
      {
         Console::WriteLine(S"The following is the exception raised: {0}", e->Message);
      }
   }

private:
   static void CreateCallee(AppDomain* myAppDomain, AssemblyBuilderAccess /*access*/)
   {
      // Create a name for the assembly.
      AssemblyName* myAssemblyName = new AssemblyName();
      myAssemblyName->Name = S"EmittedAssembly";

      // Create the dynamic assembly.
      myAssemblyBuilder = myAppDomain->DefineDynamicAssembly(myAssemblyName, 
                                             AssemblyBuilderAccess::Save);
      // Create a dynamic module.
      myModuleBuilder = myAssemblyBuilder->DefineDynamicModule(S"EmittedModule", 
                                                         S"EmittedModule.mod");
      // Create a dynamic Enum.
      myEnumBuilder = myModuleBuilder->DefineEnum(S"MyNamespace.MyEnum", 
                                 TypeAttributes::Public, __typeof(Int32));

      FieldBuilder* myFieldBuilder1 = myEnumBuilder->DefineLiteral(S"FieldOne",__box(1));
      FieldBuilder* myFieldBuilder2 = myEnumBuilder->DefineLiteral(S"FieldTwo",__box(2));

      myEnumBuilder->CreateType();
   }
};

int main()
{
   MyEnumBuilderSample::Main();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Reflection.Emit

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

EnumBuilder Members | System.Reflection.Emit Namespace

Show:
© 2014 Microsoft