This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!
Reflection Emit Core Scenarios
This section describes the core scenarios supported by Reflection Emit.
- Dynamic module defined in the caller’s static assembly. In this scenario, a dynamic module is defined in a static assembly that contains the code that creates the dynamic module. The dynamic module created this way alters the runtime representation of the assembly’s manifest. Since the disk representation of the manifest cannot be altered, the dynamic module is transient and cannot be persisted. While the dynamic module can add new COMTypes to the runtime representation of the manifest, it cannot override COMTypes that were originally defined in the manifest.
- Dynamic module defined in a static assembly that is not the caller’s assembly. In this scenario, a dynamic module is defined in a static assembly that is different from the one that contains the code that creates the dynamic module. A dynamic module created this way is transient and the restrictions that apply to the previous scenario also apply to this scenario.
- Dynamic module defined in a dynamic assembly. In this scenario, a dynamic module is defined in a dynamic assembly. A dynamic module created this way can be transient or persistable.
Restrictions on Type References
Assemblies can reference types defined in another assembly. A transient dynamic assembly can safely reference types defined in another dynamic transient assembly, a persistable dynamic assembly, or a static assembly. However, the runtime will not allow a persistable dynamic module to reference a type defined in a transient dynamic module. This is because when the persisted dynamic module is loaded subsequent to being saved to disk, the runtime will not be able to resolve the references to types defined in the transient dynamic module.
Restrictions on Emitting to Remote Application Domains
Some scenarios require a dynamic assembly to be created and executed in remote application domain. Reflection Emit does not allow a dynamic assembly to be emitted to a remote application domain. The workaround is to emit the dynamic assembly in the current application domain, save the emitted dynamic assembly to disk, and then load the dynamic assembly into the remote application domain.
Dynamic Assembly Access Modes
Dynamic assemblies can be created using one of the following access modes:
- Run. The dynamic assembly represented by an AssemblyBuilder object is transient. The dynamic assembly can only be used to execute the emitted code.
- Save. The dynamic assembly represented by an AssemblyBuilder object is persistable but cannot be executed until the saved PE has been loaded from disk.
- RunAndSave. The dynamic assembly represented by an AssemblyBuilder object is persistable but can also be executed before and/or after the assembly is saved to disk.
The access mode needs to be specified when the dynamic assembly is defined and cannot be changed at a later point in time. The runtime uses the access mode of a dynamic assembly to optimize the assembly’s internal representation.