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

列挙型の基になる型。 これは、組み込みの整数型である必要があります。

返品

定義された列挙型。

例外

表示属性以外の属性が提供されます。

-または-

指定された名前の列挙体は、このモジュールの親アセンブリに存在します。

-または-

可視性属性が列挙型のスコープと一致しません。 たとえば、visibilityNestedPublicが指定されていますが、列挙型は入れ子になった型ではありません。

namenullです。

次の例は、 DefineEnum を使用して動的モジュールに列挙クラスを実装する方法を示しています。 この例では、基になる型のInt32を持つ Elevation という名前の列挙体を定義し、値が 0 の Low と値が 1 のHighの 2 つの要素を作成します。 型が作成されると、アセンブリは 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 は、適切な型の要素を持つ列挙型を出力します。

適用対象