共用方式為


EnumBuilder.DefineLiteral(String, Object) 方法

定義

使用指定的常數值,定義列舉類型中具名的靜態欄位。

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

參數

literalName
String

靜態欄位的名稱。

literalValue
Object

常值的常數值。

傳回

定義的欄位。

範例

下列程式代碼範例示範如何使用 EnumBuilder在動態元件內建構列舉。 此範例會定義名為 Elevation的列舉,其基礎類型 Int32為 ,並建立兩個元素: Low,值為0,而 High值為1。 建立類型之後,元件會以名稱 TempAssembly.dll儲存。 您可以使用 Ildasm.exe (IL 反組譯程式) 來檢查此元件的內容。

注意

在 .NET Framework 2.0 版之前,此程式代碼範例不會產生正確的列舉。

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

備註

定義的欄位會設定欄位屬性 PublicStaticLiteral

注意

在 .NET Framework 1.0 和 1.1 版中,必須使用 來定義列舉TypeBuilder,因為EnumBuilder會發出其元素的類型Int32而非列舉型別的列舉。 在 .NET Framework 2.0 版中,EnumBuilder發出元素具有正確類型的列舉。

適用於