This documentation is archived and is not being maintained.

ModuleBuilder.DefineEnum Method

Defines an enumeration type with that is a value type with a single non-static field called value__ of the specified type.

[Visual Basic]
Public Function DefineEnum( _
   ByVal name As String, _
   ByVal visibility As TypeAttributes, _
   ByVal underlyingType As Type _
) As EnumBuilder
[C#]
public EnumBuilder DefineEnum(
 string name,
 TypeAttributes visibility,
 Type underlyingType
);
[C++]
public: EnumBuilder* DefineEnum(
 String* name,
 TypeAttributes visibility,
 Type* underlyingType
);
[JScript]
public function DefineEnum(
   name : String,
 visibility : TypeAttributes,
 underlyingType : Type
) : EnumBuilder;

Parameters

name
The full path of the enumeration type. name cannot contain embedded nulls.
visibility
The type attributes for the enumeration. The attributes are any bits defined by VisibilityMask.
underlyingType
The underlying type for the enumeration.

Return Value

Returns the defined enumeration.

Exceptions

Exception Type Condition
ArgumentException Attributes other than visibility attributes are provided.

-or-

An enum with the given name exists in the parent assembly of this module.

-or-

When the visibility attributes are incorrect for the scope of the enum. For example, if NestedPublic is specified as the visibility but the enum is not a nested type.

ArgumentNullException name is a null reference (Nothing in Visual Basic).

Remarks

The defined enum is a derived class of Enum. The defined field has Private and SpecialName set.

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of DefineEnum to implement an enumeration class in a dynamic module.

[Visual Basic] 
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName

' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain

' Create assembly in current currentDomain
myAssemblyName = New AssemblyName()
myAssemblyName.Name = "TempAssembly"

' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain.DefineDynamicAssembly _ 
                    (myAssemblyName, AssemblyBuilderAccess.Run)

' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")

' Define a enumeration type with name 'MyEnum' in the 'TempModule'.
Dim myEnumBuilder As EnumBuilder = myModuleBuilder.DefineEnum _ 
                                   ("MyEnum", TypeAttributes.Public, GetType(Integer))

' Define the named static fields in the enumeration type 'MyEnum'.
myEnumBuilder.DefineLiteral("MyEnumMember1", 2)
myEnumBuilder.DefineLiteral("MyEnumMember2", 3)
myEnumBuilder.CreateType()

[C#] 
AppDomain currentDomain;
AssemblyName myAssemblyName;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;

// Create assembly in current currentDomain
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.Run);

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");

// Define a enumeration type with name 'MyEnum' in the 'TempModule'.
EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum("MyEnum",
                     TypeAttributes.Public,typeof(int));

// Define the named static fields in the enumeration type 'MyEnum'.
myEnumBuilder.DefineLiteral("MyEnumMember1",2);
myEnumBuilder.DefineLiteral("MyEnumMember2",3);
myEnumBuilder.CreateType();


[C++] 
AppDomain* currentDomain;
AssemblyName* myAssemblyName;

// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;

// Create assembly in current currentDomain
myAssemblyName = new AssemblyName();
myAssemblyName->Name = S"TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule(S"TempModule");

// Define a enumeration type with name 'MyEnum' in the 'TempModule'.
EnumBuilder* myEnumBuilder = myModuleBuilder->DefineEnum(S"MyEnum",
   TypeAttributes::Public,__typeof(int));

// Define the named static fields in the enumeration type 'MyEnum'.
myEnumBuilder->DefineLiteral(S"MyEnumMember1",__box(2));
myEnumBuilder->DefineLiteral(S"MyEnumMember2",__box(3));
myEnumBuilder->CreateType();

[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

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

.NET Framework Security: 

See Also

ModuleBuilder Class | ModuleBuilder Members | System.Reflection.Emit Namespace

Show: