It is built in an assembly resolver callback routine. Below is a snippet.
// following are the data members in my worker class
private static ArrayList extAssemblies = new ArrayList();
private static Hashtable assembliesLookup = new Hashtable();
private static ILAssembly currentAssembly;
private String assemblyName;
private Assembly assembly;
private ArrayList classes = new ArrayList();
private ResolveEventHandler resolveEventHandler;
private static String assemblyNameToResolve;
private static ArrayList extAssemblyDirectory;
// Somewhere in you worker class upon initialization install an
// Assembly resolver.
resolveEventHandler = new ResolveEventHandler(AssemblyResolver);
Thread.GetDomain().AssemblyResolve += resolveEventHandler;
// This will now get called when .Net needs to resolve an assembly.
public Assembly AssemblyResolver(Object sender,ResolveEventArgs args)
{
String path;
// assembly is the current assembly i am loading
// get the path of the current assembly to be used
// in trying to load referenced assemblies
path =
assembly.Location.Substring(0,assembly.Location.LastIndexOf("\\") + 1);
assemblyNameToResolve = args.Name;
// just get the raw assembly name
if ((assemblyNameToResolve.IndexOf(",") != -1) &&
(assemblyNameToResolve.IndexOf("Culture") != -1))
assemblyNameToResolve =
assemblyNameToResolve.Substring(0,assemblyNameToResolve.IndexOf(","));
// see if i already looked it up
Assembly assm = assembliesLookup[assemblyNameToResolve] as Assembly;
if (assm != null)
return(assm);
try
{
// try in the current assembly directory
assm =
Assembly.LoadFrom(String.Concat(path,assemblyNameToResolve,".dll"));
}
catch (Exception)
{
}
if (assm == null)
{
// extAssemblyDirectory is a collection of paths that can be passed
// into my tool to specify additional paths where to look for
// assemblies. These would be paths to referenced assemblies
// not in the GAC or the current directory.
foreach (String assemblyPath in extAssemblyDirectory)
{
try
{
// try looking in the external path
assm =
Assembly.LoadFrom(String.Concat(assemblyPath,assemblyNameToResolve,".dll"));
if (assm != null)
break;
}
catch (Exception)
{
}
}
}
if (assm == null)
throw(new Exception("Cant find assembly " +
assemblyNameToResolve));
// add to collection to be used for finding types
extAssemblies.Add(assm);
assembliesLookup.Add(assemblyNameToResolve,assm);
assemblyNameToResolve = null;
return(assm);
}
Hope there is enough info here to help. Sorry for word wrap from the
usenet tool.
Leon Lambert
Dan said:
Leon,
Thanks - this really helps. In your code, where does extAssemblies come
from?
Thanks again,
Dan
Leon said:
Follow is a small snippet where i locate a type in asemblies.
public static Type FindType(String typeToFind)
{
// look in standard .Net types
Type ct = Type.GetType(typeToFind,false);
if (ct != null)
return(ct);
// look in the current assembly i have loaded with LoadFrom
ct = currentAssembly.assembly.GetType(typeToFind,false);
if (ct != null)
return(ct);
// look in assemblies collection built from references in the current
assembly
foreach(Assembly assm in extAssemblies)
{
ct = assm.GetType(typeToFind,false);
if (ct != null)
return(ct);
}
// brute force way by going through modules in the current assembly
Module[] mods = currentAssembly.assembly.GetModules();
foreach(Module mod in mods)
{
Type[] found = mod.FindTypes(Module.FilterTypeName, typeToFind);
if (found.Length == 1)
return(found[0]);
}
throw new Exception("Type not found " + typeToFind);
}
Hope this helps
Leon Lambert