EnumBuilder.DefineLiteral(String, Object) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Définit le champ statique nommé dans un type d’énumération avec la valeur de constante spécifiée.
public:
System::Reflection::Emit::FieldBuilder ^ DefineLiteral(System::String ^ literalName, System::Object ^ literalValue);
public System.Reflection.Emit.FieldBuilder DefineLiteral (string literalName, object? literalValue);
public System.Reflection.Emit.FieldBuilder DefineLiteral (string literalName, object literalValue);
member this.DefineLiteral : string * obj -> System.Reflection.Emit.FieldBuilder
Public Function DefineLiteral (literalName As String, literalValue As Object) As FieldBuilder
Paramètres
- literalName
- String
Nom du champ statique.
- literalValue
- Object
Valeur de constante du littéral.
Retours
Champ défini.
Exemples
L’exemple de code suivant illustre la construction d’une énumération au sein d’un assembly dynamique, à l’aide de EnumBuilder
. L’exemple définit une énumération nommée Elevation
, avec un type sous-jacent de Int32, et crée deux éléments : Low
, avec la valeur 0 et High
, avec la valeur 1. Une fois le type créé, l’assembly est enregistré avec le nom TempAssembly.dll
. Vous pouvez utiliser le Ildasm.exe (désassembleur IL) pour examiner le contenu de cet assembly.
Notes
Avant .NET Framework version 2.0, cet exemple de code ne produit pas d’énumération correcte.
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
Remarques
Le champ défini aura les attributs Publicde champ , , Staticet Literal définis.
Notes
Dans les versions 1.0 et 1.1 du .NET Framework, il est nécessaire de définir des énumérations à l’aide TypeBuilder de car EnumBuilder émet des énumérations dont les éléments sont de type Int32 au lieu du type énumération. Dans le .NET Framework version 2.0, EnumBuilder émet des énumérations dont les éléments ont le type correct.