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 열거형 클래스를 구현하는 데 사용하는 방법을 보여 줍니다. 이 예제에서는 기본 형식Int32이 있는 명명 Elevation 된 열거형을 정의하고 값이 0High이고 값이 1인 두 개의 요소를 Low만듭니다. 형식을 만든 후 어셈블리는 이름으로 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에서는 EnumBuilder 열거형이 열거형 형식 대신 Int32 형식인 열거형을 내보내므로 TypeBuilder 사용하여 열거형을 정의해야 합니다. .NET Framework 버전 2.0에서 EnumBuilder 요소가 올바른 형식의 열거형을 내보낸다.

적용 대상