Generate enumerated types dynamically (when the program is running)
enumeration type and the enumeration items contained in it are dynamically created when the program is running. This way we can put the enumeration items in an xml file such as web.config. It is easy to update at any time without recompiling the program.
Here , We need to use the System.Reflection.Emit namespace.
It provides the EnumBuilder class, which is used to dynamically create enumeration types at runtime.
(The Emit space also contains many other Builder classes, which are convenient for everyone to create when the program is running." "Assembly", "Class", "Event", etc.)
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
[ClassInterfaceAttribute(ClassInterfaceType.None)] [ComVisibleAttribute(true)] public sealed class EnumBuilder : Type, _EnumBuilder
note: |
---|
In the .NET Framework version 1.0 and 1.1, you need to use TypeBuilder to define enumerations, because EnumBuilder emits enumerations whose elements are of type Int32 rather than enumerated types. In the .NET Framework version 2.0, EnumBuilder emits enumerations whose elements have the correct type. |
The following code example demonstrates how to use EnumBuilder to construct an enumeration in a dynamic assembly . The example defines a named Elevation enumeration, its underlying type is Int32 , and creates two elements: a value of 0 Low and a value of 1 High . After creating the type, use the name TempAssembly.dll to save the assembly. You can use the MSIL disassembler (Ildasm.exe) to check the contents of this assembly.
note: |
---|
If you are using a version of the .NET Framework prior to version 2.0, this code sample will not generate the correct enumeration. |
using System; using System.Reflection; using System.Reflection.Emit; class Example { public static 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 = new 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 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 and save the assembly. Type finished = eb.CreateType(); ab.Save(aName.Name + ".dll"); foreach( 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 */
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
Windows Vista, Microsoft Windows XP SP2 Japanese Windows Server 2003 SP1 support Microsoft .NET Framework 3.0.
reference
EnumBuilder memberSystem.Reflection.Emit namespace