EnumBuilder.DefineLiteral(String, Object) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan bidang statis bernama dalam jenis enumerasi dengan nilai konstanta yang ditentukan.
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
Parameter
- literalName
- String
Nama bidang statis.
- literalValue
- Object
Nilai konstanta harfiah.
Mengembalikan
Bidang yang ditentukan.
Contoh
Contoh kode berikut menunjukkan konstruksi enumerasi dalam perakitan dinamis, menggunakan EnumBuilder
. Contoh mendefinisikan enumerasi bernama Elevation
, dengan jenis yang mendasar dari Int32, dan membuat dua elemen: Low
, dengan nilai 0, dan High
, dengan nilai 1. Setelah jenis dibuat, assembly disimpan dengan nama TempAssembly.dll
. Anda dapat menggunakan Ildasm.exe (Il Disassembler) untuk memeriksa isi perakitan ini.
Catatan
Sebelum .NET Framework versi 2.0, contoh kode ini tidak menghasilkan enumerasi yang benar.
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
Keterangan
Bidang yang ditentukan akan memiliki atribut Publicbidang , , Staticdan Literal diatur.
Catatan
Dalam versi .NET Framework 1.0 dan 1.1, perlu untuk menentukan enumerasi yang digunakan TypeBuilder karena EnumBuilder memancarkan enumerasi yang elemennya berjenis Int32 alih-alih jenis enumerasi. Dalam .NET Framework versi 2.0, EnumBuilder memancarkan enumerasi yang elemennya memiliki jenis yang benar.