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 無法包含嵌入的空。

visibility
TypeAttributes

列舉的類型屬性。 屬性為由 VisibilityMask定義的任意位元。

underlyingType
Type

列舉的底層類型。 這必須是內建的整數類型。

傳回

定義的列舉。

例外狀況

提供除可見性屬性外的其他屬性。

-或-

在該模組的父組件中存在一個帶有該名稱的枚舉。

-或-

可見性屬性與列舉範圍不符。 例如, NestedPublic 對於 ,被指定, visibility但列舉並非巢狀型別。

namenull

範例

以下範例說明了在動態模組中實作列舉類別的使用 DefineEnum 方法。 範例定義了一個名為 Elevation 的枚舉,其底層類型為 , Int32並產生兩個元素: Low值為 0 的 ,以及 High值為 1 的 。 型別建立後,集合會以名稱 TempAssembly.dll儲存為 。 你可以使用 Ildasm.exe(IL 反組譯器) 來檢查這個組建檔的內容。

Note

在 .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 設定。

欲了解更多可指定為列舉底層類型所內建整數型別的資訊,請參閱 類別函式庫概述

Note

在 .NET Framework 1.0 和 1.1 版本中,必須使用 TypeBuilder 來定義列舉,因為 EnumBuilder 會產生的列舉元素為 Int32,而非列舉型別。 在 .NET Framework 2.0 版本中,EnumBuilder 會發出具有正確型別的枚舉。

適用於