动态(程序运行时)生成枚举类型
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#
备注
示例
下面的代码示例演示了如何在动态程序集中使用 EnumBuilder 构造枚举。该示例定义一个名为Elevation 的枚举,其基础类型为 Int32,并且创建两个元素:值为 0 的 Low 和值为 1 的 High。创建完类型后,使用 TempAssembly.dll 名称来保存程序集。可以使用 MSIL
反汇编程序 (Ildasm.exe) 检查此程序集的内容。
C#
继承层次结构
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。
版本信息
请参见
EnumBuilder
成员
System.Reflection.Emit
命名空间
简介:
在程序运行时动态建立枚举类型及其包含的枚举项.这样我们就可以把枚举项放在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 版之前的版本,此代码示例不会生成正确的枚举。 |
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
命名空间
相关文章推荐
- 动态(程序运行时)生成枚举类型
- 动态(程序运行时)生成枚举类型
- 程序运行时动态生成缓存时存在的问题
- jsp实现java程序片段动态生成表格内容
- 小程序动态生成二维码,生成image图片
- Android-Activity程序动态的生成表格布局管理器
- Flink运行时之流处理程序生成流图
- Android中插件开发篇之----动态加载Activity(免安装运行程序)
- php+javascript动态显示服务器运行程序的进度条(转)
- error: 生成此程序集的运行时比当前加载的运行时新,无法加载此程序集
- vs2015+Qt5.7编译生成的程序在xp中运行提示不是有效的win32程序解决方案
- MFC/vs2005生成的exe程序,设置管理员身份运行
- VS2012生成的C++程序无法在XP上运行:
- webservice 运行时出现 无法生成临时类 类型 n 在未被引用的程序集中定义 错误
- <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序
- Linux环境中以daemon方式运行的程序生成core dump
- 对于vs中生成的sdk程序在其他电脑上无法运行的的问题
- 生成此程序集的运行时比当前加载的运行时新,无法加载此程序集
- ABAP动态生成经典应用之Dynamic SQL Excute 程序
- 在VC程序中实现控件的动态生成与响应