您的位置:首页 > 其它

动态(程序运行时)生成枚举类型

2013-01-05 14:06 239 查看
(来自:http://www.cnblogs.com/adsiz/archive/2008/01/16/1041782.html)

简介:

在程序运行时动态建立枚举类型及其包含的枚举项.这样我们就可以把枚举项放在web.config这样的xml文件中.便于随时更新,同时还不用重新编译程序.

这里,我们要用到System.Reflection.Emit
命名空间.

它提供了EnumBuilder类,用来在运行是动态建立枚举类型.

(Emit空间中还包含了许多其他Builder类,方便大家在程序运行时建立"程序集","类","事件"等等)

EnumBuilder 类
说明并表示枚举类型。

命名空间: System.Reflection.Emit

程序集: mscorlib(在 mscorlib.dll 中)


语法

C#

[ClassInterfaceAttribute(ClassInterfaceType.None)] 

[ComVisibleAttribute(true)] 

public sealed class EnumBuilder : Type, _EnumBuilder



备注


注意:
在 .NET Framework 1.0 版和 1.1 版中,需要使用 TypeBuilder 定义枚举,因为 EnumBuilder 发出其元素属于 Int32 类型而非枚举类型的枚举。在
.NET Framework 2.0 版中,EnumBuilder 发出其元素具有正确类型的枚举。

示例

下面的代码示例演示了如何在动态程序集中使用 EnumBuilder 构造枚举。该示例定义一个名为Elevation 的枚举,其基础类型为 Int32,并且创建两个元素:值为 0 的 Low 和值为 1 的 High。创建完类型后,使用 TempAssembly.dll 名称来保存程序集。可以使用 MSIL
反汇编程序 (Ildasm.exe) 检查此程序集的内容。


注意:
如果使用 .NET Framework 2.0 版之前的版本,此代码示例不会生成正确的枚举。
C#

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 

*/



继承层次结构

System.Object

System.Reflection.MemberInfo

System.Type

System.Reflection.Emit.EnumBuilder


线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。


平台

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 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。


版本信息

.NET Framework

受以下版本支持:3.0、2.0、1.1、1.0


请参见


参考

EnumBuilder
成员

System.Reflection.Emit
命名空间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: