ModuleBuilder 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義並代表動態元件中的模組。
public ref class ModuleBuilder : System::Reflection::Module
public ref class ModuleBuilder abstract : System::Reflection::Module
public ref class ModuleBuilder : System::Reflection::Module, System::Runtime::InteropServices::_ModuleBuilder
public class ModuleBuilder : System.Reflection.Module
public abstract 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
type ModuleBuilder = class
inherit 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
Public Class ModuleBuilder
Inherits Module
Public MustInherit Class ModuleBuilder
Inherits Module
Public Class ModuleBuilder
Inherits Module
Implements _ModuleBuilder
- 繼承
- 屬性
- 實作
範例
下列程式代碼範例示範如何使用 ModuleBuilder
來建立動態模組。 請注意,ModuleBuilder 是在 AssemblyBuilder中呼叫 DefineDynamicModule,而不是透過建構函式來建立。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
public ref class CodeGenerator
{
private:
AssemblyBuilder^ myAssemblyBuilder;
public:
CodeGenerator()
{
// Get the current application domain for the current thread.
AppDomain^ myCurrentDomain = AppDomain::CurrentDomain;
AssemblyName^ myAssemblyName = gcnew 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", String::typeid, FieldAttributes::Public );
array<Type^>^myMethodArgs = {String::typeid};
// Add 'MyMethod' method to the class, with the specified attribute and signature.
MethodBuilder^ myMethod = myTypeBuilder->DefineMethod( "MyMethod", MethodAttributes::Public, CallingConventions::Standard, nullptr, 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();
}
property AssemblyBuilder^ MyAssembly
{
AssemblyBuilder^ get()
{
return this->myAssemblyBuilder;
}
}
};
int main()
{
CodeGenerator^ myCodeGenerator = gcnew 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 :{0}", 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 );
array<Object^>^args = {"Hello."};
Object^ myObject = Activator::CreateInstance( myType, nullptr, nullptr );
myMethodInfo->Invoke( myObject, args );
}
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 方法。
建構函式
ModuleBuilder() |
初始化 ModuleBuilder 類別的新實例。 |
屬性
Assembly |
取得定義這個實例 ModuleBuilder的動態元件。 |
Assembly | (繼承來源 Module) |
CustomAttributes |
取得集合,其中包含此模組的自定義屬性。 (繼承來源 Module) |
FullyQualifiedName |
取得 |
MDStreamVersion |
取得元數據數據流版本。 |
MDStreamVersion |
取得元數據數據流版本。 (繼承來源 Module) |
MetadataToken |
取得識別元數據中目前動態模組的令牌。 |
MetadataToken |
取得識別元數據中模組的令牌。 (繼承來源 Module) |
ModuleHandle |
取得模組的句柄。 (繼承來源 Module) |
ModuleVersionId |
取得通用唯一標識碼 (UUID),可用來區分模組的兩個版本。 |
ModuleVersionId |
取得通用唯一標識碼 (UUID),可用來區分模組的兩個版本。 (繼承來源 Module) |
Name |
字串,表示這是記憶體內部模組。 |
Name |
取得 |
ScopeName |
取得字串,表示動態模組的名稱。 |
ScopeName |
取得表示模組名稱的字串。 (繼承來源 Module) |
方法
明確介面實作
擴充方法
GetCustomAttribute(Module, Type) |
擷取套用至指定模組之指定型別的自定義屬性。 |
GetCustomAttribute<T>(Module) |
擷取套用至指定模組之指定型別的自定義屬性。 |
GetCustomAttributes(Module) |
擷取套用至指定模組的自定義屬性集合。 |
GetCustomAttributes(Module, Type) |
擷取套用至指定模組之指定型別的自定義屬性集合。 |
GetCustomAttributes<T>(Module) |
擷取套用至指定模組之指定型別的自定義屬性集合。 |
IsDefined(Module, Type) |
指出指定的型別的自定義屬性是否套用至指定的模組。 |
GetModuleVersionId(Module) |
定義並代表動態元件中的模組。 |
HasModuleVersionId(Module) |
定義並代表動態元件中的模組。 |