EnumBuilder.DefineLiteral(String, Object) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Define el campo estático con nombre en un tipo de enumeración con el valor constante especificado.
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
Parámetros
- literalName
- String
El nombre del campo estático.
- literalValue
- Object
El valor constante del literal.
Devoluciones
Campo definido.
Ejemplos
En el ejemplo de código siguiente se muestra la construcción de una enumeración dentro de un ensamblado dinámico, mediante EnumBuilder
. En el ejemplo se define una enumeración denominada Elevation
, con un tipo subyacente de Int32y se crean dos elementos: Low
, con un valor de 0 y High
, con un valor de 1. Una vez creado el tipo, el ensamblado se guarda con el nombre TempAssembly.dll
. Puede usar el Ildasm.exe (Desensamblador de IL) para examinar el contenido de este ensamblado.
Nota
Antes de la versión 2.0 de .NET Framework, este ejemplo de código no genera una enumeración correcta.
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
Comentarios
El campo definido tendrá los atributos Publicde campo , Staticy Literal establecido.
Nota
En las versiones 1.0 y 1.1 de .NET Framework, es necesario definir enumeraciones mediante TypeBuilder porque EnumBuilder emite enumeraciones cuyos elementos son de tipo Int32 en lugar del tipo de enumeración. En .NET Framework versión 2.0, EnumBuilder emite enumeraciones cuyos elementos tienen el tipo correcto.