英語で読む

次の方法で共有


ModuleBuilder.DefineEnum(String, TypeAttributes, Type) メソッド

定義

指定した型の value__ という単一の非静的フィールドと共に、値型の列挙型を定義します。

C#
public System.Reflection.Emit.EnumBuilder DefineEnum(string name, System.Reflection.TypeAttributes visibility, Type underlyingType);

パラメーター

name
String

列挙型の完全なパス。 name に埋め込み null 値を含めることはできません。

visibility
TypeAttributes

列挙型の型属性。 属性は、VisibilityMask で定義された任意のビットです。

underlyingType
Type

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

戻り値

定義された列挙型。

例外

可視属性以外の属性が指定されています。

- または -

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

- または -

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

namenullです。

次の例は、 を使用 DefineEnum して動的モジュールに列挙クラスを実装する方法を示しています。 この例では、 という名前 Elevation の列挙体を定義し、基になる型が の Int322 つの要素を作成します Low。値は 0、値 Highは 1 です。 型が作成されると、アセンブリは という名前 TempAssembly.dllで保存されます。 Ildasm.exe (IL 逆アセンブラー) を使用して、このアセンブリの内容を調べることができます。

注意

.NET Framework バージョン 2.0 より前のコード例では、正しい列挙体は生成されません。

C#
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
 */

注釈

定義された列挙型は の派生クラス Enumです。 フィールドには value__ および PrivateSpecialName 属性が設定されています。

基になる列挙型として指定できる組み込みの整数型の詳細については、「 クラス ライブラリの概要」を参照してください。

注意

.NET Framework バージョン 1.0 および 1.1 では、 を使用して列挙型をTypeBuilder定義する必要があります。これは、列挙型の代わりに要素が型Int32である列挙型を出力するためEnumBuilderです。 .NET Framework バージョン 2.0 では、EnumBuilder要素の型が正しい列挙型を出力します。

注意

.NET Framework 2.0 Service Pack 1 以降では、このメンバーは フラグをReflectionPermissionFlag.ReflectionEmit使用する必要ReflectionPermissionがなくなりました。 (リフレクション出力のセキュリティの問題に関するページを参照してください)。この機能を使用するには、アプリケーションで .NET Framework 3.5 以降をターゲットにする必要があります。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1