Mihir Solanki

mihirsolanki.com

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


Navigation

Links

Subscriptions

Post Categories



.NET Framework FAQ : 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.

A type is resolved dynamically by defining a handler for the TypeResolve event. The TypeResolve event is defined as follows:

public event ResolveEventHandler TypeResolve;

A resource is resolved dynamically by defining a handler for the ResourceResolve event. The ResourceResolve event is defined as follows:

public event ResolveEventHandler ResourceResolve;

For both types and resources, the handler must resolve the assembly in which the entity is defined. The code for the handler is very similar to the handler shown in the sample above.

posted on Tuesday, December 20, 2005 5:05 PM by mihirsolanki





Powered by Dot Net Junkies, by Telligent Systems