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

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

简介:
      在程序运行时动态建立枚举类型及其包含的枚举项.这样我们就可以把枚举项放在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 命名空间
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值