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으로 지정되었지만 열거형이 중첩 형식이 아닌 경우가 여기에 해당합니다.

name이(가) null인 경우

예제

다음 예제에서는 를 사용하여 DefineEnum 동적 모듈에서 열거형 클래스를 구현하는 방법을 보여 줍니다. 이 예제에서는 의 기본 형식Int32을 가진 라는 Elevation 열거형을 정의하고 값이 0인 , 값High이 1인 두 개의 요소를 Low만듭니다. 형식을 만든 후 어셈블리는 이름으로 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__ 필드에 및 PrivateSpecialName 특성이 설정되어 있습니다.

기본 열거형 형식으로 지정할 수 있는 기본 제공 정수 형식에 대한 자세한 내용은 클래스 라이브러리 개요를 참조하세요.

참고

.NET Framework 버전 1.0 및 1.1에서는 요소가 열거형 형식이 아닌 형식 Int32 인 열거형을 내보내므로 를 사용하여 TypeBuilderEnumBuilder 열거형을 정의해야 합니다. .NET Framework 버전 2.0 EnumBuilder 에서는 요소가 올바른 형식의 열거형을 내보낸다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 멤버는 ReflectionPermission 더 이상 플래그가 ReflectionPermissionFlag.ReflectionEmit 필요하지 않습니다. (리플렉션 내보내기의 보안 문제를 참조하세요.) 이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상