Bewerken

Share via


AssemblyBuilder.DefineDynamicModule Method

Definition

Defines a dynamic module in this assembly.

Overloads

DefineDynamicModule(String)

Defines a named transient dynamic module in this assembly.

DefineDynamicModule(String, Boolean)

Defines a named transient dynamic module in this assembly and specifies whether symbol information should be emitted.

DefineDynamicModule(String, String)

Defines a persistable dynamic module with the given name that will be saved to the specified file. No symbol information is emitted.

DefineDynamicModule(String, String, Boolean)

Defines a persistable dynamic module, specifying the module name, the name of the file to which the module will be saved, and whether symbol information should be emitted using the default symbol writer.

DefineDynamicModule(String)

Source:
AssemblyBuilder.cs
Source:
AssemblyBuilder.cs
Source:
AssemblyBuilder.cs

Defines a named transient dynamic module in this assembly.

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

Parameters

name
String

The name of the dynamic module.

Returns

A ModuleBuilder representing the defined dynamic module.

Exceptions

name begins with white space.

-or-

The length of name is zero.

-or-

The length of name is greater than the system-defined maximum length.

-or-

.NET Framework only: A module with the same name has already been defined in this assembly.

name is null.

.NET Core and .NET 5+ only: A dynamic module has already been defined in this assembly.

The caller does not have the required permission.

The assembly for default symbol writer cannot be loaded.

-or-

The type that implements the default symbol writer interface cannot be found.

Examples

The code example below demonstrates how to create a transient dynamic module using 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")

Remarks

.NET Core and .NET 5+ only: Multi-module assemblies are not supported. Only one dynamic module may be defined in an assembly.

.NET Framework only: It is an error to define multiple dynamic modules with the same name in an assembly.

The defined dynamic module is transient. The dynamic module is not saved, even if the parent dynamic assembly was created with RunAndSave.

Note

To suppress optimizations when debugging dynamic modules, apply the DebuggableAttribute attribute to the dynamic assembly before calling DefineDynamicModule. Create an instance of DebuggableAttribute with the DisableOptimizations flag and apply it using the SetCustomAttribute method. The attribute must be applied to the dynamic assembly. It has no effect if applied to the module.

Note

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

Applies to

DefineDynamicModule(String, Boolean)

Defines a named transient dynamic module in this assembly and specifies whether symbol information should be emitted.

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

Parameters

name
String

The name of the dynamic module.

emitSymbolInfo
Boolean

true if symbol information is to be emitted; otherwise, false.

Returns

A ModuleBuilder representing the defined dynamic module.

Exceptions

name begins with white space.

-or-

The length of name is zero.

-or-

The length of name is greater than the system-defined maximum length.

name is null.

The assembly for default symbol writer cannot be loaded.

-or-

The type that implements the default symbol writer interface cannot be found.

The caller does not have the required permission.

Examples

The code example below demonstrates how to create a transient dynamic module using DefineDynamicModule, suppressing symbol information.

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)

Remarks

It is an error to define multiple dynamic modules with the same name in an assembly.

The dynamic module is not saved, even if the parent dynamic assembly was created with RunAndSave.

Note

To suppress optimizations when debugging dynamic modules, apply the DebuggableAttribute attribute to the dynamic assembly before calling DefineDynamicModule. Create an instance of DebuggableAttribute with the DisableOptimizations flag and apply it using the SetCustomAttribute method. The attribute must be applied to the dynamic assembly. It has no effect if applied to the module.

Note

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

Applies to

DefineDynamicModule(String, String)

Defines a persistable dynamic module with the given name that will be saved to the specified file. No symbol information is emitted.

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

Parameters

name
String

The name of the dynamic module.

fileName
String

The name of the file to which the dynamic module should be saved.

Returns

A ModuleBuilder object representing the defined dynamic module.

Exceptions

name or fileName is null.

The length of name or fileName is zero.

-or-

The length of name is greater than the system-defined maximum length.

-or-

fileName contains a path specification (a directory component, for example).

-or-

There is a conflict with the name of another file that belongs to this assembly.

This assembly has been previously saved.

This assembly was called on a dynamic assembly with Run attribute.

The caller does not have the required permission.

The assembly for default symbol writer cannot be loaded.

-or-

The type that implements the default symbol writer interface cannot be found.

Examples

The code example below demonstrates how to create a persistent dynamic module using 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")

Remarks

To define a persistable dynamic module, this assembly needs to be created with the Save or the RunAndSave attribute.

If you want the module to contain the assembly manifest, name should be the same as the name of the assembly (that is, the AssemblyName.Name property of the AssemblyName used to create the dynamic assembly) and fileName should be the same as the filename you specify when you save the assembly.

In an assembly with only one module, that module should contain the assembly manifest.

Note

To suppress optimizations when debugging dynamic modules, apply the DebuggableAttribute attribute to the dynamic assembly before calling DefineDynamicModule. Create an instance of DebuggableAttribute with the DisableOptimizations flag and apply it using the SetCustomAttribute method. The attribute must be applied to the dynamic assembly. It has no effect if applied to the module.

Note

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

Applies to

DefineDynamicModule(String, String, Boolean)

Defines a persistable dynamic module, specifying the module name, the name of the file to which the module will be saved, and whether symbol information should be emitted using the default symbol writer.

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

Parameters

name
String

The name of the dynamic module.

fileName
String

The name of the file to which the dynamic module should be saved.

emitSymbolInfo
Boolean

If true, symbolic information is written using the default symbol writer.

Returns

A ModuleBuilder object representing the defined dynamic module.

Exceptions

name or fileName is null.

The length of name or fileName is zero.

-or-

The length of name is greater than the system-defined maximum length.

-or-

fileName contains a path specification (a directory component, for example).

-or-

There is a conflict with the name of another file that belongs to this assembly.

This assembly has been previously saved.

This assembly was called on a dynamic assembly with the Run attribute.

The assembly for default symbol writer cannot be loaded.

-or-

The type that implements the default symbol writer interface cannot be found.

The caller does not have the required permission.

Examples

The code example below demonstrates how to create a persistent dynamic module with symbol emission using 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)

Remarks

To define a persistable dynamic module, this assembly needs to be created with the Save or the RunAndSave attribute.

If you want the module to contain the assembly manifest, name should be the same as the name of the assembly (that is, the AssemblyName.Name property of the AssemblyName used to create the dynamic assembly) and fileName should be the same as the filename you specify when you save the assembly.

In an assembly with only one module, that module should contain the assembly manifest.

Note

To suppress optimizations when debugging dynamic modules, apply the DebuggableAttribute attribute to the dynamic assembly before calling DefineDynamicModule. Create an instance of DebuggableAttribute with the DisableOptimizations flag and apply it using the SetCustomAttribute method. The attribute must be applied to the dynamic assembly. It has no effect if applied to the module.

Note

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

Applies to