Monday, June 29, 2009

How do I dynamically resolve assemblies, types, and resources?

The assembly resolution is controlled by the configuration properties for the application domain such as System.AppDomainSetup.ApplicationBase. The configuration properties may not be sufficient in some hosting scenarios, especially if the host is creating assemblies in memory on the fly using Reflection Emit, since there may not be an assembly on disk to find. In such cases, you can use the System.AssemblyResolve event to hook into the type loading process.
The AssemblyResolve event is defined as follows:
public event ResolveEventHandler AssemblyResolve
where System.ResolveEventHandler is defined as follows:
public delegate Assembly ResolveEventHandler(Object sender, ResolveEventArgs args)
The args parameter to ResolveEventHandler is the identity of the assembly the runtime is seeking. The receipient of the event is free to resolve the reference to the assembly by any means. For example, the receipient may construct an assembly on the fly, find it in a custom location on disk, etc. The only requirement is that the receipient return a instance of System.Reflection.Assembly.

No comments: