ModuleBuilder.DefineEnum(String, TypeAttributes, Type) 方法

定义

定义一个枚举类型,该类型是具有一个调用指定类型的非静态字段 value__ 的值类型。

public:
 System::Reflection::Emit::EnumBuilder ^ DefineEnum(System::String ^ name, System::Reflection::TypeAttributes visibility, Type ^ underlyingType);
public System.Reflection.Emit.EnumBuilder DefineEnum(string name, System.Reflection.TypeAttributes visibility, Type underlyingType);
member this.DefineEnum : string * System.Reflection.TypeAttributes * Type -> System.Reflection.Emit.EnumBuilder
Public Function DefineEnum (name As String, visibility As TypeAttributes, underlyingType As Type) As EnumBuilder

参数

name
String

枚举类型的完整路径。 name 不能包含嵌入的 null。

visibility
TypeAttributes

枚举的类型属性。 这些属性是由 . 定义的 VisibilityMask任意位。

underlyingType
Type

枚举的基础类型。 这必须是内置整数类型。

返回

定义的枚举。

例外

提供除可见性属性以外的属性。

-或-

此模块的父程序集中存在具有给定名称的枚举。

-或-

可见性属性与枚举的范围不匹配。 例如, NestedPublic 指定了 visibility枚举,但枚举不是嵌套类型。

namenull

示例

以下示例演示如何在 DefineEnum 动态模块中实现枚举类。 该示例定义一个名为基础类型的Elevation枚举Int32,并创建两个元素:Low值为 0,值为 High1。 创建类型后,将用名称 TempAssembly.dll保存程序集。 可以使用 Ildasm.exe(IL 反汇编程序) 检查此程序集的内容。

注释

在 .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
 */
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

注解

定义的枚举是派生类 。Enum 字段 value__Private 设置和 SpecialName 属性。

有关可指定为枚举的基础类型的内置整数类型的详细信息,请参阅 类库概述

注释

在 .NET Framework 版本 1.0 和 1.1 中,必须使用 TypeBuilder 定义枚举,因为 EnumBuilder发出其元素的类型为 Int32 而不是枚举类型的枚举。 在 .NET Framework 版本 2.0 中,EnumBuilder发出其元素具有正确类型的枚举。

适用于