ModuleBuilder.DefineEnum(String, TypeAttributes, Type) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Definuje typ výčtu, který je typ hodnoty s jedním nestatického pole volaného 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 nemůže obsahovat vložené hodnoty null.
- visibility
- TypeAttributes
Atributy typu pro výčet. Atributy jsou všechny bity definované .VisibilityMask
- underlyingType
- Type
Základní typ výčtu. Musí se jednat o předdefinovaný celočíselnou typ.
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í rozsahu výčtu. Například NestedPublic je zadán pro visibility, ale výčet není vnořený typ.
name je null.
Příklady
Následující příklad znázorňuje použití k implementaci třídy výčtu DefineEnum 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 ).
Note
Před .NET Framework verze 2.0 tento příklad kódu nevytváří správný výčet.
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ýčt je odvozená třída Enum. Pole value__ obsahuje Private a SpecialName atributy jsou nastaveny.
Další informace o předdefinovaných typech celých čísel, které lze zadat jako základní typy výčtů, naleznete v tématu Přehled knihovny tříd.
Note
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 namísto typu výčtu. V rozhraní .NET Framework verze 2.0 EnumBuilder generuje výčty, jejichž prvky mají správný typ.