ModuleBuilder.DefineEnum(String, TypeAttributes, Type) Metoda

Definice

Definuje typ výčtu, který je typ hodnoty s jedním nestatického pole s názvem value__ zadaného typu.

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

Parametry

name
String

Úplná cesta typu výčtu. name nesmí obsahovat vložené hodnoty null.

visibility
TypeAttributes

Atributy typu pro výčet. Atributy jsou všechny bity definované nástrojem VisibilityMask.

underlyingType
Type

Základní typ výčtu. Toto musí být předdefinované celé číslo.

Návraty

Definovaný výčet.

Výjimky

Jsou k dispozici jiné atributy než atributy viditelnosti.

-nebo-

Výčet s daným názvem existuje v nadřazené sestavení tohoto modulu.

-nebo-

Atributy viditelnosti neodpovídají oboru výčtu. Je například NestedPublic zadán pro visibility, ale výčet není vnořeným typem.

name je null.

Příklady

Následující příklad ukazuje použití DefineEnum k implementaci třídy výčtu v dynamickém modulu. Příklad definuje výčet s názvem Elevation , který má základní typ Int32, a vytvoří dva prvky: Low, s hodnotou 0 a High, s hodnotou 1. Po vytvoření typu se sestavení uloží s názvem TempAssembly.dll. K prozkoumání obsahu tohoto sestavení můžete použít Ildasm.exe (IL Disassembler ).

Poznámka

Před rozhraním .NET Framework verze 2.0 tento příklad kódu nevytváří správný výčet.

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

Poznámky

Definovaný výčet je odvozenou třídou .Enum Pole value__Private nastavené atributy a SpecialName .

Další informace o předdefinovaných celočíselných typech, které lze zadat jako podkladové typy výčtů, najdete v tématu Přehled knihovny tříd.

Poznámka

V rozhraní .NET Framework verze 1.0 a 1.1 je nutné definovat výčty pomocí TypeBuilder , protože EnumBuilder generuje výčty, jejichž prvky jsou typu Int32 místo typu výčtu. V rozhraní .NET Framework verze 2.0 generuje výčty, EnumBuilder jejichž prvky mají správný typ.

Poznámka

Počínaje rozhraním .NET Framework 2.0 Service Pack 1 už tento člen nevyžaduje ReflectionPermission s příznakem ReflectionPermissionFlag.ReflectionEmit . (Viz Problémy se zabezpečením v emitu reflexe.) Chcete-li použít tuto funkci, vaše aplikace by měla cílit na rozhraní .NET Framework 3.5 nebo novější.

Platí pro