ModuleBuilder.DefineEnum(String, TypeAttributes, Type) Metoda

Definicja

Definiuje typ wyliczenia, który jest typem wartości z pojedynczym polem niestatycznym nazywanym value__ określonym typem.

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

Pełna ścieżka typu wyliczenia. name nie może zawierać osadzonych wartości null.

visibility
TypeAttributes

Atrybuty typu dla wyliczenia. Atrybuty są dowolnymi bitami zdefiniowanymi przez VisibilityMaskelement .

underlyingType
Type

Podstawowy typ wyliczenia. Musi to być wbudowany typ liczby całkowitej.

Zwraca

Zdefiniowana wyliczenie.

Wyjątki

Dostępne są atrybuty inne niż atrybuty widoczności.

-lub-

W zestawie nadrzędnym tego modułu istnieje wyliczenie o podanej nazwie.

-lub-

Atrybuty widoczności nie są zgodne z zakresem wyliczenia. Na przykład NestedPublic określono parametr visibility, ale wyliczenie nie jest typem zagnieżdżonym.

name to null.

Przykłady

W poniższym przykładzie pokazano użycie DefineEnum metody implementowania klasy wyliczenia w module dynamicznym. W przykładzie zdefiniowano wyliczenie o nazwie Elevation , która ma podstawowy typ Int32, i tworzy dwa elementy: Low, z wartością 0 i High, z wartością 1. Po utworzeniu typu zestaw zostanie zapisany przy użyciu nazwy TempAssembly.dll. Aby sprawdzić zawartość tego zestawu, można użyć Ildasm.exe (IL Dezasembler ).

Uwaga

Przed .NET Framework w wersji 2.0 ten przykład kodu nie generuje poprawnego wyliczenia.

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

Uwagi

Zdefiniowana wyliczenie jest klasą pochodną klasy Enum. Pole value__ ma Private ustawione atrybuty i .SpecialName

Aby uzyskać więcej informacji na temat wbudowanych typów liczb całkowitych, które można określić jako podstawowe typy wyliczenia, zobacz Omówienie biblioteki klas.

Uwaga

W .NET Framework wersji 1.0 i 1.1 należy zdefiniować wyliczenia przy użyciu, TypeBuilder ponieważ EnumBuilder emituje wyliczenia, których elementy są typu Int32 zamiast typu wyliczenia. W .NET Framework wersji 2.0 EnumBuilder emituje wyliczenia, których elementy mają poprawny typ.

Uwaga

Począwszy od .NET Framework dodatku Service Pack 2.0, ten element członkowski nie wymaga ReflectionPermission już flagiReflectionPermissionFlag.ReflectionEmit. (Zobacz Problemy z zabezpieczeniami w emitu odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.

Dotyczy