ModuleBuilder 類別

定義

定義並表示動態組件中的模組。

public ref class ModuleBuilder : System::Reflection::Module, System::Runtime::InteropServices::_ModuleBuilder
public ref class ModuleBuilder : System::Reflection::Module
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public class ModuleBuilder : System.Reflection.Module, System.Runtime.InteropServices._ModuleBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public class ModuleBuilder : System.Reflection.Module, System.Runtime.InteropServices._ModuleBuilder
public class ModuleBuilder : System.Reflection.Module
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type ModuleBuilder = class
    inherit Module
    interface _ModuleBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ModuleBuilder = class
    inherit Module
    interface _ModuleBuilder
type ModuleBuilder = class
    inherit Module
Public Class ModuleBuilder
Inherits Module
Implements _ModuleBuilder
Public Class ModuleBuilder
Inherits Module
繼承
ModuleBuilder
屬性
實作

範例

以下範例程式碼示範如何使用 來 ModuleBuilder 建立動態模組。 請注意,ModuleBuilder 是透過呼叫 DefineDynamicModuleAssemblyBuilder非透過建構子建立的。

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public class CodeGenerator
{
   AssemblyBuilder myAssemblyBuilder;
   public CodeGenerator()
   {
      // Get the current application domain for the current thread.
      AppDomain myCurrentDomain = AppDomain.CurrentDomain;
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "TempAssembly";

      // Define a dynamic assembly in the current application domain.
      myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
                     (myAssemblyName, AssemblyBuilderAccess.Run);

      // Define a dynamic module in this assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.
                                      DefineDynamicModule("TempModule");

      // Define a runtime class with specified name and attributes.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType
                                       ("TempClass",TypeAttributes.Public);

      // Add 'Greeting' field to the class, with the specified attribute and type.
      FieldBuilder greetingField = myTypeBuilder.DefineField("Greeting",
                                                            typeof(String), FieldAttributes.Public);
      Type[] myMethodArgs = { typeof(String) };

      // Add 'MyMethod' method to the class, with the specified attribute and signature.
      MethodBuilder myMethod = myTypeBuilder.DefineMethod("MyMethod",
         MethodAttributes.Public, CallingConventions.Standard, null,myMethodArgs);

      ILGenerator methodIL = myMethod.GetILGenerator();
      methodIL.EmitWriteLine("In the method...");
      methodIL.Emit(OpCodes.Ldarg_0);
      methodIL.Emit(OpCodes.Ldarg_1);
      methodIL.Emit(OpCodes.Stfld, greetingField);
      methodIL.Emit(OpCodes.Ret);
      myTypeBuilder.CreateType();
   }
   public AssemblyBuilder MyAssembly
   {
      get
      {
         return this.myAssemblyBuilder;
      }
   }
}
public class TestClass
{
   public static void Main()
   {
      CodeGenerator myCodeGenerator = new CodeGenerator();
      // Get the assembly builder for 'myCodeGenerator' object.
      AssemblyBuilder myAssemblyBuilder = myCodeGenerator.MyAssembly;
      // Get the module builder for the above assembly builder object .
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.
                                                           GetDynamicModule("TempModule");
      Console.WriteLine("The fully qualified name and path to this "
                               + "module is :" +myModuleBuilder.FullyQualifiedName);
      Type myType = myModuleBuilder.GetType("TempClass");
      MethodInfo myMethodInfo =
                                                myType.GetMethod("MyMethod");
       // Get the token used to identify the method within this module.
      MethodToken myMethodToken =
                        myModuleBuilder.GetMethodToken(myMethodInfo);
      Console.WriteLine("Token used to identify the method of 'myType'"
                    + " within the module is {0:x}",myMethodToken.Token);
     object[] args={"Hello."};
     object myObject = Activator.CreateInstance(myType,null,null);
     myMethodInfo.Invoke(myObject,args);
   }
}
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Class CodeGenerator
   Private myAssemblyBuilder As AssemblyBuilder

   Public Sub New()
      ' Get the current application domain for the current thread.
      Dim myCurrentDomain As AppDomain = AppDomain.CurrentDomain
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "TempAssembly"

      ' Define a dynamic assembly in the current application domain.
      myAssemblyBuilder = _
               myCurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)

      ' Define a dynamic module in this assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")

      ' Define a runtime class with specified name and attributes.
      Dim myTypeBuilder As TypeBuilder = _
               myModuleBuilder.DefineType("TempClass", TypeAttributes.Public)

      ' Add 'Greeting' field to the class, with the specified attribute and type.
      Dim greetingField As FieldBuilder = _
               myTypeBuilder.DefineField("Greeting", GetType(String), FieldAttributes.Public)
      Dim myMethodArgs As Type() = {GetType(String)}

      ' Add 'MyMethod' method to the class, with the specified attribute and signature.
      Dim myMethod As MethodBuilder = _
               myTypeBuilder.DefineMethod("MyMethod", MethodAttributes.Public, _
               CallingConventions.Standard, Nothing, myMethodArgs)

      Dim methodIL As ILGenerator = myMethod.GetILGenerator()
      methodIL.EmitWriteLine("In the method...")
      methodIL.Emit(OpCodes.Ldarg_0)
      methodIL.Emit(OpCodes.Ldarg_1)
      methodIL.Emit(OpCodes.Stfld, greetingField)
      methodIL.Emit(OpCodes.Ret)
      myTypeBuilder.CreateType()
   End Sub

   Public ReadOnly Property MyAssembly() As AssemblyBuilder
      Get
         Return Me.myAssemblyBuilder
      End Get
   End Property
End Class

Public Class TestClass
   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
      Dim myCodeGenerator As New CodeGenerator()
      ' Get the assembly builder for 'myCodeGenerator' object.
      Dim myAssemblyBuilder As AssemblyBuilder = myCodeGenerator.MyAssembly
      ' Get the module builder for the above assembly builder object .
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.GetDynamicModule("TempModule")
      Console.WriteLine("The fully qualified name and path to this " + _
                        "module is :" + myModuleBuilder.FullyQualifiedName)
      Dim myType As Type = myModuleBuilder.GetType("TempClass")
      Dim myMethodInfo As MethodInfo = myType.GetMethod("MyMethod")
      ' Get the token used to identify the method within this module.
      Dim myMethodToken As MethodToken = myModuleBuilder.GetMethodToken(myMethodInfo)
      Console.WriteLine("Token used to identify the method of 'myType'" + _
                        " within the module is {0:x}", myMethodToken.Token)
      Dim args As Object() = {"Hello."}
      Dim myObject As Object = Activator.CreateInstance(myType, Nothing, Nothing)
      myMethodInfo.Invoke(myObject, args)
   End Sub
End Class

備註

要取得 的 ModuleBuilder實例,請使用該 AssemblyBuilder.DefineDynamicModule 方法。

屬性

名稱 Description
Assembly

取得定義此實例 ModuleBuilder的動態組裝。

Assembly

在此實例Module中,得到適當的Assembly

(繼承來源 Module)
CustomAttributes

會得到一個包含該模組自訂屬性的集合。

(繼承來源 Module)
FullyQualifiedName

會得到 String 代表完整資格名稱和通往此模組的路徑。

MDStreamVersion

取得元資料串流版本。

MDStreamVersion

取得元資料串流版本。

(繼承來源 Module)
MetadataToken

會取得一個標記,用來識別當前動態模組的元資料。

MetadataToken

會獲得一個標記,用來識別模組的元資料。

(繼承來源 Module)
ModuleHandle

拿到模組的控制權。

(繼承來源 Module)
ModuleVersionId

獲得一個通用唯一識別碼(UUID),可用來區分模組的兩個版本。

ModuleVersionId

獲得一個通用唯一識別碼(UUID),可用來區分模組的兩個版本。

(繼承來源 Module)
Name

一個字串表示這是記憶體模組。

Name

會得到 String 代表移除路徑的模組名稱的 。

(繼承來源 Module)
ScopeName

會得到一個代表動態模組名稱的字串。

ScopeName

會得到一個代表模組名稱的字串。

(繼承來源 Module)

方法

名稱 Description
CreateGlobalFunctions()

完成此動態模組的全域函數定義與全域資料定義。

DefineDocument(String, Guid, Guid, Guid)

定義一份文件作為來源。

DefineEnum(String, TypeAttributes, Type)

定義一種列舉型別,該值型別有一個指定的非靜態欄位,稱為 value__ 該型別。

DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])

定義一個全域方法,包含指定的名稱、屬性、呼叫慣例、回傳類型、返回類型的自訂修飾符、參數類型,以及參數類型的自訂修飾符。

DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[])

定義一個全域方法,包含指定的名稱、屬性、呼叫慣例、回傳型別及參數型別。

DefineGlobalMethod(String, MethodAttributes, Type, Type[])

定義一個全域方法,包含指定的名稱、屬性、回傳類型及參數類型。

DefineInitializedData(String, Byte[], FieldAttributes)

在可攜式可執行檔(PE)檔案的 .sdata 區段定義已初始化的資料欄位。

DefineManifestResource(String, Stream, ResourceAttributes)

定義一個二進位大型物件(BLOB),代表一個要嵌入動態組裝中的清單資源。

DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

定義 PInvoke 一個方法,包含指定名稱、定義該方法的 DLL 名稱、方法的屬性、方法的呼叫慣例、方法的回傳類型、參數的類型,以及 PInvoke 旗標。

DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

定義 PInvoke 一個方法,包含指定名稱、定義該方法的 DLL 名稱、方法的屬性、方法的呼叫慣例、方法的回傳類型、參數的類型,以及 PInvoke 旗標。

DefineResource(String, String, ResourceAttributes)

定義了指定管理的嵌入式資源,並包含將儲存在本模組中的指定屬性。

DefineResource(String, String)

定義了儲存在此模組中的指定管理內嵌資源。

DefineType(String, TypeAttributes, Type, Int32)

構造給 TypeBuilder 定的型別名稱、屬性、定義型別所延伸的型態,以及該型別的總大小。

DefineType(String, TypeAttributes, Type, PackingSize, Int32)

構造 TypeBuilder 給定的型別名稱、屬性、定義型別所延伸的型別、該型別的打包大小,以及該型別的總大小。

DefineType(String, TypeAttributes, Type, PackingSize)

構造給 TypeBuilder 定的型別名稱、屬性、該定義型別所延伸的型態,以及該型態的包裝大小。

DefineType(String, TypeAttributes, Type, Type[])

建構 TypeBuilder 給定的型別名稱、屬性、該定義型別所延伸的型態,以及該型別實作的介面。

DefineType(String, TypeAttributes, Type)

構造給 TypeBuilder 定型別名稱、其屬性,以及該定義型別所延伸的型別。

DefineType(String, TypeAttributes)

構造給 TypeBuilder 定的類型名稱與類型屬性。

DefineType(String)

在此模組中構造 TypeBuilder 一個私有型別,且名稱為指定名稱。

DefineUninitializedData(String, Int32, FieldAttributes)

在可攜式可執行檔(PE)檔案的 .sdata 區段定義一個未初始化的資料欄位。

DefineUnmanagedResource(Byte[])

定義一個未受管理的嵌入式資源,給定一個不透明的二進位大型物件(BLOB)位元組。

DefineUnmanagedResource(String)

定義一個名為 Win32 資源檔案的非管理資源。

Equals(Object)

回傳一個值,表示該實例是否等於指定物件。

Equals(Object)

判斷此模組與指定物件是否相等。

(繼承來源 Module)
FindTypes(TypeFilter, Object)

回傳由給定濾波器及濾波條件接受的類別陣列。

(繼承來源 Module)
GetArrayMethod(Type, String, CallingConventions, Type, Type[])

回傳陣列類別中命名的方法。

GetArrayMethodToken(Type, String, CallingConventions, Type, Type[])

回傳陣列類別中命名方法的標記。

GetConstructorToken(ConstructorInfo, IEnumerable<Type>)

回傳用於識別此模組中具有指定屬性與參數類型的建構子的標記。

GetConstructorToken(ConstructorInfo)

回傳用於識別該模組中指定建構子的標記。

GetCustomAttributes(Boolean)

回傳所有已套用於目前 ModuleBuilder的自訂屬性。

GetCustomAttributes(Boolean)

回傳所有自訂屬性。

(繼承來源 Module)
GetCustomAttributes(Type, Boolean)

回傳所有已套用於當前 ModuleBuilder的自訂屬性,且這些屬性源自指定的屬性類型。

GetCustomAttributes(Type, Boolean)

取得指定類型的自訂屬性。

(繼承來源 Module)
GetCustomAttributesData()

回傳已套用於當前 ModuleBuilder物件的 CustomAttributeData 屬性資訊。

GetCustomAttributesData()

回傳當前模組的物件清單 CustomAttributeData ,可用於僅反射的上下文。

(繼承來源 Module)
GetField(String, BindingFlags)

回傳一個模組層級欄位,定義於可攜式執行檔(PE)的 .sdata 區域,該欄位包含指定的名稱與綁定屬性。

GetField(String, BindingFlags)

回傳一個具有指定名稱與綁定屬性的欄位。

(繼承來源 Module)
GetField(String)

回傳一個欄位名稱與指定相同。

(繼承來源 Module)
GetFields()

回傳模組上定義的全域場。

(繼承來源 Module)
GetFields(BindingFlags)

回傳可攜式執行檔(PE)檔案 .sdata 區域中定義且符合指定綁定標誌的所有欄位。

GetFields(BindingFlags)

回傳模組中定義且符合指定綁定標誌的全域欄位。

(繼承來源 Module)
GetFieldToken(FieldInfo)

回傳用於識別該模組中指定欄位的標記。

GetHashCode()

傳回這個實例的哈希碼。

GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Module)
GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

回傳一個包含指定名稱、綁定資訊、呼叫慣例,以及參數類型與修飾符的方法。

(繼承來源 Module)
GetMethod(String, Type[])

回傳一個具有指定名稱與參數類型的方法。

(繼承來源 Module)
GetMethod(String)

回傳一個名稱相同的方法。

(繼承來源 Module)
GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

回傳符合指定條件的模組層級方法。

GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

依照指定標準回傳方法實作。

(繼承來源 Module)
GetMethods()

回傳模組中定義的全域方法。

(繼承來源 Module)
GetMethods(BindingFlags)

回傳所有在模組層 ModuleBuilder級定義且符合指定綁定標誌的方法。

GetMethods(BindingFlags)

回傳模組上定義且符合指定綁定標誌的全域方法。

(繼承來源 Module)
GetMethodToken(MethodInfo, IEnumerable<Type>)

回傳用於識別此模組中具有指定屬性與參數類型的方法的標記。

GetMethodToken(MethodInfo)

回傳用於識別該模組中指定方法的標記。

GetObjectData(SerializationInfo, StreamingContext)

提供 ISerializable 序列化物件的實作。

(繼承來源 Module)
GetPEKind(PortableExecutableKinds, ImageFileMachine)

會得到一對表示模組程式碼性質及模組目標平台的值。

GetPEKind(PortableExecutableKinds, ImageFileMachine)

會得到一對表示模組程式碼性質及模組目標平台的值。

(繼承來源 Module)
GetSignatureToken(Byte[], Int32)

定義一個具有指定字元陣列與簽名長度的簽章標記。

GetSignatureToken(SignatureHelper)

定義一個由指定 SignatureHelper所定義的簽名標記。

GetSignerCertificate()

回傳 X509Certificate 一個物件,對應於該模組所屬組件的 Authenticode 簽章中所包含的憑證。 若集會尚未簽署 Authenticode, null 則會被退回。

GetSignerCertificate()

回傳 X509Certificate 一個物件,對應於該模組所屬組件的 Authenticode 簽章中所包含的憑證。 若集會尚未簽署 Authenticode, null 則會被退回。

(繼承來源 Module)
GetStringConstant(String)

回傳模組常數池中給定字串的標記。

GetSymWriter()

回傳與此動態模組相關的符號寫入器。

GetType()

取得目前實例的 Type

(繼承來源 Object)
GetType(String, Boolean, Boolean)

取得模組中定義的命名型別,可選擇性地忽略型別名稱的情況。 若找不到該型別,則可選擇拋出例外。

GetType(String, Boolean, Boolean)

回傳指定的類型,並指定是否要對模組進行區分大小寫的搜尋,以及如果找不到該型別是否拋出例外。

(繼承來源 Module)
GetType(String, Boolean)

取得模組中定義的命名型別,可選擇性地忽略型別名稱的情況。

GetType(String, Boolean)

回傳指定的類型,並搜尋具有指定大小寫區分的模組。

(繼承來源 Module)
GetType(String)

取得模組中定義的命名型別。

GetType(String)

回傳指定的類型,執行區分大小寫的搜尋。

(繼承來源 Module)
GetTypes()

回傳此模組中定義的所有類別。

GetTypes()

回傳此模組中定義的所有型別。

(繼承來源 Module)
GetTypeToken(String)

回傳用來識別指定名稱型別的標記。

GetTypeToken(Type)

回傳用於識別該模組中指定類型的標記。

IsDefined(Type, Boolean)

回傳一個值,表示該模組是否已套用指定的屬性類型。

IsDefined(Type, Boolean)

回傳一個值,表示該模組是否已套用指定的屬性類型。

(繼承來源 Module)
IsResource()

會得到一個值,表示該物件是否為資源。

IsResource()

會得到一個值,表示該物件是否為資源。

(繼承來源 Module)
IsTransient()

回傳一個表示此動態模組是否為暫態模組的值。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ResolveField(Int32, Type[], Type[])

回傳由指定中繼資料標記所識別的欄位,並依指定通用型別參數定義的上下文。

ResolveField(Int32, Type[], Type[])

回傳由指定中繼資料標記所識別的欄位,並依指定通用型別參數定義的上下文。

(繼承來源 Module)
ResolveField(Int32)

回傳由指定元資料標記所識別的欄位。

(繼承來源 Module)
ResolveMember(Int32, Type[], Type[])

回傳由指定元資料標記所識別的類型或成員,並依指定通用類型參數定義的上下文。

ResolveMember(Int32, Type[], Type[])

回傳由指定元資料標記所識別的類型或成員,並依指定通用類型參數定義的上下文。

(繼承來源 Module)
ResolveMember(Int32)

回傳由指定元資料標記所識別的類型或成員。

(繼承來源 Module)
ResolveMethod(Int32, Type[], Type[])

回傳由指定中繼資料標記所識別的方法或建構子,並依指定泛型參數定義的上下文。

ResolveMethod(Int32, Type[], Type[])

回傳由指定中繼資料標記所識別的方法或建構子,並依指定泛型參數定義的上下文。

(繼承來源 Module)
ResolveMethod(Int32)

回傳由指定中繼資料標記所識別的方法或建構子。

(繼承來源 Module)
ResolveSignature(Int32)

回傳由元資料標記識別的簽名塊。

ResolveSignature(Int32)

回傳由元資料標記識別的簽名塊。

(繼承來源 Module)
ResolveString(Int32)

回傳由指定元資料標記所識別的字串。

ResolveString(Int32)

回傳由指定元資料標記所識別的字串。

(繼承來源 Module)
ResolveType(Int32, Type[], Type[])

回傳由指定元資料標記所識別的類型,並在指定的通用類型參數所定義的上下文中。

ResolveType(Int32, Type[], Type[])

回傳由指定元資料標記所識別的類型,並在指定的通用類型參數所定義的上下文中。

(繼承來源 Module)
ResolveType(Int32)

回傳由指定元資料標記所識別的類型。

(繼承來源 Module)
SetCustomAttribute(ConstructorInfo, Byte[])

透過指定的二進位大型物件(BLOB)來代表該屬性,為此模組套用自訂屬性。

SetCustomAttribute(CustomAttributeBuilder)

透過使用自訂屬性建構器,套用自訂屬性給這個模組。

SetSymCustomAttribute(String, Byte[])

這種方法沒什麼用。

SetUserEntryPoint(MethodInfo)

設定使用者的進入點。

ToString()

回傳模組名稱。

(繼承來源 Module)

明確介面實作

名稱 Description
_Module.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Module)
_Module.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Module)
_Module.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Module)
_Module.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Module)
_ModuleBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

關於此成員的描述,請參見 GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

_ModuleBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

關於此成員的描述,請參見 GetTypeInfo(UInt32, UInt32, IntPtr)

_ModuleBuilder.GetTypeInfoCount(UInt32)

關於此成員的描述,請參見 GetTypeInfoCount(UInt32)

_ModuleBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

關於此成員的描述,請參見 Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

擴充方法

名稱 Description
GetCustomAttribute(Module, Type)

擷取指定型別的自訂屬性,套用到指定模組。

GetCustomAttribute<T>(Module)

擷取指定型別的自訂屬性,套用到指定模組。

GetCustomAttributes(Module, Type)

擷取一組指定型別的自訂屬性,這些屬性套用到指定模組上。

GetCustomAttributes(Module)

擷取一套套用到指定模組的自訂屬性集合。

GetCustomAttributes<T>(Module)

擷取一組指定型別的自訂屬性,這些屬性套用到指定模組上。

IsDefined(Module, Type)

表示是否套用特定類型的自訂屬性於特定模組。

適用於