共用方式為


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

定義

定義列舉型別,此列舉型別為實值型別 (Value 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

列舉型別的基礎型別。 這必須是內建整數型別 (Integer Type)。

傳回

已定義的列舉型別。

例外狀況

提供可視性屬性 (Attribute) 以外的屬性。

-或-

具有指定名稱的列舉型別存在於這個模組的父組件中。

-或-

可視性屬性不符合列舉型別範圍。 例如,NestedPublic 已指定給 visibility,但是列舉型別並非巢狀型別。

namenull

範例

下列範例說明如何在 DefineEnum 動態模組中實作列舉類別。 此範例會定義名為 Elevation 的列舉,其基礎類型 Int32為 ,並建立兩個元素: Low、值為 0 和 High,且值為 1。 建立類型之後,元件會以名稱 TempAssembly.dll儲存。 您可以使用 Ildasm.exe (IL 反組譯程式) 來檢查此元件的內容。

注意

在 .NET Framework 2.0 版之前,此程式代碼範例不會產生正確的列舉。

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

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 = gcnew 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 Int32.
    EnumBuilder^ eb = 
            mb->DefineEnum("Elevation", TypeAttributes::Public, int::typeid);
      
    // Define two members, "High" and "Low".
    eb->DefineLiteral("Low", (Object^) 0);
    eb->DefineLiteral("High", 1);

    // Create the type and save the assembly.
    Type^ finished = eb->CreateType();
    ab->Save(aName->Name + ".dll");

    for each (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
 */
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發出元素具有正確類型的列舉。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermissionReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出中的安全性問題 ) 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於