Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ModuleBuilder::DefineEnum Method (String^, TypeAttributes, Type^)

 

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

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

public:
EnumBuilder^ DefineEnum(
	String^ name,
	TypeAttributes visibility,
	Type^ underlyingType
)

Parameters

name
Type: System::String^

The full path of the enumeration type. name cannot contain embedded nulls.

visibility
Type: System.Reflection::TypeAttributes

The type attributes for the enumeration. The attributes are any bits defined by VisibilityMask.

underlyingType
Type: System::Type^

The underlying type for the enumeration. This must be a built-in integer type.

Return Value

Type: System.Reflection.Emit::EnumBuilder^

The defined enumeration.

Exception Condition
ArgumentException

Attributes other than visibility attributes are provided.

-or-

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

-or-

The visibility attributes do not match the scope of the enumeration. For example, NestedPublic is specified for visibility, but the enumeration is not a nested type.

ArgumentNullException

name is null.

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

For more information about the built-in integer types that can be specified as the underlying types of enumerations, see .NET Framework Class Library Overview.

System_CAPS_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.

System_CAPS_noteNote

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag::ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

The following example illustrates the use of DefineEnum to implement an enumeration class in a dynamic module. The example defines an enumeration named Elevation that has 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 Ildasm.exe (IL Disassembler) to examine the contents of this assembly.

System_CAPS_noteNote

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

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

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 = gcnew 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 Int32.
    EnumBuilder^ eb = 
            mb->DefineEnum("Elevation", TypeAttributes::Public, int::typeid);

    // Define two members, "High" and "Low".
    eb->DefineLiteral("Low", (Object^) 0);
    eb->DefineLiteral("High", 1);

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

    for each (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
 */

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show:
© 2017 Microsoft