共用方式為


AssemblyBuilder.DefineDynamicModule 方法

定義

在此元件中定義動態模組。

多載

DefineDynamicModule(String)

在此元件中定義具名的暫時性動態模組。

DefineDynamicModule(String, Boolean)

定義這個元件中的具名暫時性動態模組,並指定是否應該發出符號資訊。

DefineDynamicModule(String, String)

使用將儲存至指定檔案的指定名稱,定義可保存的動態模組。 不會發出符號資訊。

DefineDynamicModule(String, String, Boolean)

定義可保存的動態模組、指定模組名稱、將儲存模組的檔名,以及是否應該使用預設符號寫入器發出符號資訊。

DefineDynamicModule(String)

來源:
AssemblyBuilder.cs
來源:
AssemblyBuilder.cs
來源:
AssemblyBuilder.cs

在此元件中定義具名的暫時性動態模組。

public:
 System::Reflection::Emit::ModuleBuilder ^ DefineDynamicModule(System::String ^ name);
public System.Reflection.Emit.ModuleBuilder DefineDynamicModule (string name);
member this.DefineDynamicModule : string -> System.Reflection.Emit.ModuleBuilder
Public Function DefineDynamicModule (name As String) As ModuleBuilder

參數

name
String

動態模組的名稱。

傳回

表示已定義動態模組的 ModuleBuilder

例外狀況

name 開頭為空格符。

-或-

name 的長度為零。

-或-

name 長度大於系統定義的最大長度。

-或-

僅限 .NET Framework:已在此元件中定義具有相同 name 的模組。

namenull

僅限 .NET Core 和 .NET 5+ :此元件中已定義動態模組。

呼叫端沒有必要的許可權。

無法載入預設符號寫入器的元件。

-或-

找不到實作預設符號寫入器介面的類型。

範例

下列程式代碼範例示範如何使用 DefineDynamicModule建立暫時性動態模組。

AppDomain^ myAppDomain = Thread::GetDomain();
AssemblyName^ myAsmName = gcnew AssemblyName;
myAsmName->Name = "MyAssembly";
AssemblyBuilder^ myAsmBuilder = myAppDomain->DefineDynamicAssembly(
   myAsmName, AssemblyBuilderAccess::Run );

// Create a transient dynamic module. Since no DLL name is specified with
// this constructor, it cannot be saved.
ModuleBuilder^ myModuleBuilder = myAsmBuilder->DefineDynamicModule( "MyModule1" );
AppDomain myAppDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyAssembly";
AssemblyBuilder myAsmBuilder = myAppDomain.DefineDynamicAssembly(
                     myAsmName,
                     AssemblyBuilderAccess.Run);

// Create a transient dynamic module. Since no DLL name is specified with
// this constructor, it can not be saved.
ModuleBuilder myModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule1");
Dim myAppDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyAssembly"
Dim myAsmBuilder As AssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAsmName, _
                      AssemblyBuilderAccess.Run)

' Create a transient dynamic module. Since no DLL name is specified with
' this constructor, it can not be saved. 
Dim myModuleBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule1")

備註

僅限 .NET Core 和 .NET 5+:不支援 多模組元件。 元件中只能定義一個動態模組。

僅限 .NET Framework:在元件中定義多個具有相同名稱的動態模組是錯誤的。

定義的動態模組是暫時性的。 即使使用 RunAndSave建立父動態元件,也不會儲存動態模組。

注意

若要在偵錯動態模組時隱藏優化,請在呼叫 DefineDynamicModule之前,將 DebuggableAttribute 屬性套用至動態元件。 使用 DisableOptimizations 旗標建立 DebuggableAttribute 實例,並使用 SetCustomAttribute 方法加以套用。 屬性必須套用至動態元件。 如果套用至模組,則不會有任何作用。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要使用 ReflectionPermissionFlag.ReflectionEmit 旗標 ReflectionPermission。 (請參閱反映發出中的 安全性問題。若要使用這項功能,您的應用程式應該以 .NET Framework 3.5 或更新版本為目標。

適用於

DefineDynamicModule(String, Boolean)

定義這個元件中的具名暫時性動態模組,並指定是否應該發出符號資訊。

public:
 System::Reflection::Emit::ModuleBuilder ^ DefineDynamicModule(System::String ^ name, bool emitSymbolInfo);
public System.Reflection.Emit.ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo);
member this.DefineDynamicModule : string * bool -> System.Reflection.Emit.ModuleBuilder
Public Function DefineDynamicModule (name As String, emitSymbolInfo As Boolean) As ModuleBuilder

參數

name
String

動態模組的名稱。

emitSymbolInfo
Boolean

如果要發出符號資訊,true;否則,false

傳回

表示已定義動態模組的 ModuleBuilder

例外狀況

name 開頭為空格符。

-或-

name 的長度為零。

-或-

name 長度大於系統定義的最大長度。

namenull

無法載入預設符號寫入器的元件。

-或-

找不到實作預設符號寫入器介面的類型。

呼叫端沒有必要的許可權。

範例

下列程式代碼範例示範如何使用 DefineDynamicModule來建立暫時性動態模組,並隱藏符號資訊。

AppDomain^ myAppDomain = Thread::GetDomain();
AssemblyName^ myAsmName = gcnew AssemblyName;
myAsmName->Name = "MyAssembly";
AssemblyBuilder^ myAsmBuilder = myAppDomain->DefineDynamicAssembly(
   myAsmName, AssemblyBuilderAccess::Run );

// Create a transient dynamic module. Since no DLL name is specified with
// this constructor, it can not be saved. By specifying the second parameter
// of the constructor as false, we can suppress the emission of symbol info.
ModuleBuilder^ myModuleBuilder = myAsmBuilder->DefineDynamicModule(
   "MyModule2", false );
AppDomain myAppDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyAssembly";
AssemblyBuilder myAsmBuilder = myAppDomain.DefineDynamicAssembly(
                     myAsmName,
                     AssemblyBuilderAccess.Run);

// Create a transient dynamic module. Since no DLL name is specified with
// this constructor, it can not be saved. By specifying the second parameter
// of the constructor as false, we can suppress the emission of symbol info.
ModuleBuilder myModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule2",
                                 false);
Dim myAppDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyAssembly"
Dim myAsmBuilder As AssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAsmName, _
                      AssemblyBuilderAccess.Run)

' Create a transient dynamic module. Since no DLL name is specified with
' this constructor, it can not be saved. By specifying the second parameter
' of the constructor as false, we can suppress the emission of symbol info.
Dim myModuleBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule2", _
                                  False)

備註

在元件中定義多個具有相同名稱的動態模組是錯誤的。

即使使用 RunAndSave建立父動態元件,也不會儲存動態模組。

注意

若要在偵錯動態模組時隱藏優化,請在呼叫 DefineDynamicModule之前,將 DebuggableAttribute 屬性套用至動態元件。 使用 DisableOptimizations 旗標建立 DebuggableAttribute 實例,並使用 SetCustomAttribute 方法加以套用。 屬性必須套用至動態元件。 如果套用至模組,則不會有任何作用。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要使用 ReflectionPermissionFlag.ReflectionEmit 旗標 ReflectionPermission。 (請參閱反映發出中的 安全性問題。若要使用這項功能,您的應用程式應該以 .NET Framework 3.5 或更新版本為目標。

適用於

DefineDynamicModule(String, String)

使用將儲存至指定檔案的指定名稱,定義可保存的動態模組。 不會發出符號資訊。

public:
 System::Reflection::Emit::ModuleBuilder ^ DefineDynamicModule(System::String ^ name, System::String ^ fileName);
public System.Reflection.Emit.ModuleBuilder DefineDynamicModule (string name, string fileName);
member this.DefineDynamicModule : string * string -> System.Reflection.Emit.ModuleBuilder
Public Function DefineDynamicModule (name As String, fileName As String) As ModuleBuilder

參數

name
String

動態模組的名稱。

fileName
String

動態模組應儲存至其中的檔名。

傳回

代表已定義動態模組的 ModuleBuilder 物件。

例外狀況

namefileNamenull

namefileName 的長度為零。

-或-

name 長度大於系統定義的最大長度。

-或-

fileName 包含路徑規格(例如目錄元件)。

-或-

與屬於這個元件的另一個檔名發生衝突。

此元件先前已儲存。

此元件是在具有 Run 屬性的動態元件上呼叫的。

呼叫端沒有必要的許可權。

無法載入預設符號寫入器的元件。

-或-

找不到實作預設符號寫入器介面的類型。

範例

下列程式代碼範例示範如何使用 DefineDynamicModule建立永續性動態模組。

AppDomain^ myAppDomain = Thread::GetDomain();
AssemblyName^ myAsmName = gcnew AssemblyName;
myAsmName->Name = "MyAssembly";
AssemblyBuilder^ myAsmBuilder = myAppDomain->DefineDynamicAssembly(
   myAsmName, AssemblyBuilderAccess::Run );

// Create a dynamic module that can be saved as the specified DLL name.
ModuleBuilder^ myModuleBuilder = myAsmBuilder->DefineDynamicModule(
   "MyModule3", "MyModule3.dll" );
AppDomain myAppDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyAssembly";
AssemblyBuilder myAsmBuilder = myAppDomain.DefineDynamicAssembly(
                     myAsmName,
                     AssemblyBuilderAccess.Run);
// Create a dynamic module that can be saved as the specified DLL name.
ModuleBuilder myModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule3",
                                 "MyModule3.dll");
Dim myAppDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyAssembly"
Dim myAsmBuilder As AssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAsmName, _
                      AssemblyBuilderAccess.Run)
' Create a dynamic module that can be saved as the specified DLL name.
Dim myModuleBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule3", _
                              "MyModule3.dll")

備註

若要定義可保存的動態模組,必須使用 SaveRunAndSave 屬性來建立此元件。

如果您想要讓模組包含元件指令清單,name 應該與元件名稱相同(也就是用來建立動態元件之 AssemblyNameAssemblyName.Name 屬性),而 fileName 應該與您儲存元件時指定的檔名相同。

在只有一個模組的元件中,該模組應該包含元件指令清單。

注意

若要在偵錯動態模組時隱藏優化,請在呼叫 DefineDynamicModule之前,將 DebuggableAttribute 屬性套用至動態元件。 使用 DisableOptimizations 旗標建立 DebuggableAttribute 實例,並使用 SetCustomAttribute 方法加以套用。 屬性必須套用至動態元件。 如果套用至模組,則不會有任何作用。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要使用 ReflectionPermissionFlag.ReflectionEmit 旗標 ReflectionPermission。 (請參閱反映發出中的 安全性問題。若要使用這項功能,您的應用程式應該以 .NET Framework 3.5 或更新版本為目標。

適用於

DefineDynamicModule(String, String, Boolean)

定義可保存的動態模組、指定模組名稱、將儲存模組的檔名,以及是否應該使用預設符號寫入器發出符號資訊。

public:
 System::Reflection::Emit::ModuleBuilder ^ DefineDynamicModule(System::String ^ name, System::String ^ fileName, bool emitSymbolInfo);
public System.Reflection.Emit.ModuleBuilder DefineDynamicModule (string name, string fileName, bool emitSymbolInfo);
member this.DefineDynamicModule : string * string * bool -> System.Reflection.Emit.ModuleBuilder
Public Function DefineDynamicModule (name As String, fileName As String, emitSymbolInfo As Boolean) As ModuleBuilder

參數

name
String

動態模組的名稱。

fileName
String

動態模組應儲存至其中的檔名。

emitSymbolInfo
Boolean

如果 true,則會使用預設符號寫入器來撰寫符號資訊。

傳回

代表已定義動態模組的 ModuleBuilder 物件。

例外狀況

namefileNamenull

namefileName 的長度為零。

-或-

name 長度大於系統定義的最大長度。

-或-

fileName 包含路徑規格(例如目錄元件)。

-或-

與屬於這個元件的另一個檔名發生衝突。

此元件先前已儲存。

此元件是在具有 Run 屬性的動態元件上呼叫的。

無法載入預設符號寫入器的元件。

-或-

找不到實作預設符號寫入器介面的類型。

呼叫端沒有必要的許可權。

範例

下列程式代碼範例示範如何使用 DefineDynamicModule建立具有符號發射的持續性動態模組。

AppDomain^ myAppDomain = Thread::GetDomain();
AssemblyName^ myAsmName = gcnew AssemblyName;
myAsmName->Name = "MyAssembly";
AssemblyBuilder^ myAsmBuilder = myAppDomain->DefineDynamicAssembly(
   myAsmName, AssemblyBuilderAccess::Run );

// Create a dynamic module that can be saved as the specified DLL name. By
// specifying the third parameter as true, we can allow the emission of symbol info.
ModuleBuilder^ myModuleBuilder = myAsmBuilder->DefineDynamicModule(
   "MyModule4", "MyModule4.dll", true );
AppDomain myAppDomain = Thread.GetDomain();
AssemblyName myAsmName = new AssemblyName();
myAsmName.Name = "MyAssembly";
AssemblyBuilder myAsmBuilder = myAppDomain.DefineDynamicAssembly(
                     myAsmName,
                     AssemblyBuilderAccess.Run);
// Create a dynamic module that can be saved as the specified DLL name. By
// specifying the third parameter as true, we can allow the emission of symbol info.
ModuleBuilder myModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule4",
                                 "MyModule4.dll",
                                  true);
Dim myAppDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyAssembly"
Dim myAsmBuilder As AssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAsmName, _
                      AssemblyBuilderAccess.Run)
' Create a dynamic module that can be saved as the specified DLL name. By
' specifying the third parameter as true, we can allow the emission of symbol info.
Dim myModuleBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule4", _
                              "MyModule4.dll", _
                              True)

備註

若要定義可保存的動態模組,必須使用 SaveRunAndSave 屬性來建立此元件。

如果您想要讓模組包含元件指令清單,name 應該與元件名稱相同(也就是用來建立動態元件之 AssemblyNameAssemblyName.Name 屬性),而 fileName 應該與您儲存元件時指定的檔名相同。

在只有一個模組的元件中,該模組應該包含元件指令清單。

注意

若要在偵錯動態模組時隱藏優化,請在呼叫 DefineDynamicModule之前,將 DebuggableAttribute 屬性套用至動態元件。 使用 DisableOptimizations 旗標建立 DebuggableAttribute 實例,並使用 SetCustomAttribute 方法加以套用。 屬性必須套用至動態元件。 如果套用至模組,則不會有任何作用。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要使用 ReflectionPermissionFlag.ReflectionEmit 旗標 ReflectionPermission。 (請參閱反映發出中的 安全性問題。若要使用這項功能,您的應用程式應該以 .NET Framework 3.5 或更新版本為目標。

適用於