Share via


ModuleBuilder.DefineEnum Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines an enumeration type in this module.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function DefineEnum ( _
    name As String, _
    visibility As TypeAttributes, _
    underlyingType As Type _
) As EnumBuilder
[SecuritySafeCriticalAttribute]
public EnumBuilder DefineEnum(
    string name,
    TypeAttributes visibility,
    Type underlyingType
)

Parameters

  • name
    Type: System.String
    The full name of the enumeration type, including the namespace. name cannot contain embedded nulls.
  • 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.

Exceptions

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 nulla null reference (Nothing in Visual Basic).

Remarks

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

Examples

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.

Imports System.Reflection
Imports System.Reflection.Emit

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

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

      ' Create a dynamic assembly in the current application domain, 
      ' and allow it to be executed.
      Dim aName As AssemblyName = New AssemblyName("TempAssembly")
      Dim ab As AssemblyBuilder = currentDomain.DefineDynamicAssembly( _
          aName, AssemblyBuilderAccess.Run)

      ' Define a dynamic module in "TempAssembly" assembly. The module can
      ' have the same name as the assembly.
      Dim mb As ModuleBuilder = _
          ab.DefineDynamicModule(aName.Name)

      ' Define a public enumeration with the name "Elevation" and an 
      ' underlying type of Integer.
      Dim eb As EnumBuilder = _
          mb.DefineEnum("Elevation", TypeAttributes.Public, GetType(Integer))

      ' Define two members, "High" and "Low".
      eb.DefineLiteral("Low", 0)
      eb.DefineLiteral("High", 1)

      ' Create the type.
      Dim finished As Type = eb.CreateType()

      For Each fi As FieldInfo in finished.GetFields()
         outputBlock.Text += String.Format("{0}.{1} = {2}" & vbCrLf, _
            finished.Name, fi.Name, fi.GetRawConstantValue())
      Next
   End Sub
End Class

' This code example produces the following output:
'
'Elevation.Low = 0
'Elevation.High = 1 
using System;
using System.Reflection;
using System.Reflection.Emit;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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.
      AssemblyName aName = new AssemblyName("TempAssembly");
      AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
          aName, AssemblyBuilderAccess.Run);

      // Define a dynamic module in "TempAssembly" assembly. The module can
      // have the same name as the assembly.
      ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);

      // 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.
      Type finished = eb.CreateType();

      foreach (FieldInfo fi in finished.GetFields())
      {
         outputBlock.Text += String.Format("{0}.{1} = {2}\n", 
            finished.Name, fi.Name, fi.GetRawConstantValue());
      }
   }
}

/* This code example produces the following output:

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

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.