Reflection Emit Dynamic Assembly Scenarios
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Reflection Emit Dynamic Assembly Scenarios 

Reflection emit supports the creation of dynamic modules defined in dynamic assemblies. A dynamic module created in a dynamic assembly 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 transient dynamic assembly, a persistable dynamic assembly, or a static assembly. However, the common language runtime does 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 after being saved to disk, the runtime cannot 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 a remote application domain. Reflection emit does not allow a dynamic assembly to be emitted directly to a remote application domain. The solution 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 is transient. The dynamic assembly can only be used to execute the emitted code.

  • Save

    The dynamic assembly represented by an AssemblyBuilder is persistable but cannot be executed until the saved portable executable (PE) file has been loaded from disk.

  • RunAndSave

    The dynamic assembly represented by an AssemblyBuilder is persistable but can also be executed before and/or after the assembly is saved to disk.

The access mode must be specified when the dynamic assembly is defined and cannot be changed later. The runtime uses the access mode of a dynamic assembly to optimize the assembly's internal representation.

See Also

© 2016 Microsoft