EnumBuilder Class
Describes and represents an enumeration type.
Assembly: mscorlib (in mscorlib.dll)
Note: |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
Note: |
|---|
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.
Note: |
|---|
Prior to the .NET Framework version 2.0, this code example does not produce a correct enumeration. |
Imports System Imports System.Reflection Imports System.Reflection.Emit Module Example Sub Main() ' 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 and saved to disk. Dim aName As AssemblyName = New AssemblyName("TempAssembly") Dim ab As AssemblyBuilder = 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. Dim mb As ModuleBuilder = _ ab.DefineDynamicModule(aName.Name, aName.Name & ".dll") ' 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 and save the assembly. Dim finished As Type = eb.CreateType() ab.Save(aName.Name & ".dll") For Each o As Object In [Enum].GetValues(finished) Console.WriteLine("{0}.{1} = {2}", finished, o, CInt(o)) Next End Sub End Module ' This code example produces the following output: ' 'Elevation.Low = 0 'Elevation.High = 1
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: