ModuleBuilder.DefineEnum(String, TypeAttributes, Type) Metode

Definisi

Menentukan jenis enumerasi yang merupakan jenis nilai dengan satu bidang non-statis yang disebut value__ jenis yang ditentukan.

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

Parameter

name
String

Jalur lengkap jenis enumerasi. name tidak boleh berisi null yang disematkan.

visibility
TypeAttributes

Atribut jenis untuk enumerasi. Atribut adalah bit apa pun yang ditentukan oleh VisibilityMask.

underlyingType
Type

Jenis yang mendasar untuk enumerasi. Ini harus berupa jenis bilangan bulat bawaan.

Mengembalikan

Enumerasi yang ditentukan.

Pengecualian

Atribut selain atribut visibilitas disediakan.

-atau-

Enumerasi dengan nama yang diberikan ada di rakitan induk modul ini.

-atau-

Atribut visibilitas tidak cocok dengan cakupan enumerasi. Misalnya, NestedPublic ditentukan untuk visibility, tetapi enumerasi bukan jenis berlapis.

name adalah null.

Contoh

Contoh berikut mengilustrasikan penggunaan DefineEnum untuk mengimplementasikan kelas enumerasi dalam modul dinamis. Contoh mendefinisikan enumerasi bernama Elevation yang memiliki jenis Int32dasar , dan membuat dua elemen: Low, dengan nilai 0, dan High, dengan nilai 1. Setelah jenis dibuat, rakitan disimpan dengan nama TempAssembly.dll. Anda dapat menggunakan Ildasm.exe (Il Disassembler) untuk memeriksa konten rakitan ini.

Note

Sebelum .NET Framework versi 2.0, contoh kode ini tidak menghasilkan enumerasi yang benar.

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

Enum yang ditentukan adalah kelas turunan dari Enum. Bidang value__ memiliki Private atribut dan SpecialName diatur.

Untuk informasi selengkapnya tentang jenis bilangan bulat bawaan yang dapat ditentukan sebagai jenis enumerasi yang mendasar, lihat Gambaran Umum Pustaka Kelas.

Note

Dalam .NET Framework versi 1.0 dan 1.1, perlu untuk menentukan enumerasi menggunakan 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.

Berlaku untuk