Reflection - Loading assemblies with unloaded references

G

Guest

I have the predicament of having to load several assemblies on the fly and
when I do so, I get an exception stating that one of the referenced
assemblies cannot be found. Is there any way to recurse into an assembly to
discover the referenced assemblies without actually loading the root assembly
where I start the load?
For example:

assembly 1 contains references to assembly 1_1 and assembly 1_2
assembly 1_1 contains reference to assembly 2
assembly 1_2 contains reference to assembly 3

If I attempt to load assembly 1 in order to get the AssemblyNames[] of it's
referenced assemblies, I get an exception on the Load() call because the
referenced assemblies cannot be found. I have them all in the same directory,
by the way.
 
V

Vijay

To my knowledge no.. But what you can do is handle the missing assembly
exception and try to locate the assembly, copy it to the path, and start
your code. Also remember that .. the search path, current executable path,
GAC and so on.. You can alter this with App.Config / make it search in a
specific path for your dll's. If you still have the assembly in the same
path and getting the error.. , you might want to check the complete
reference hierarchy out.. There must be something broken in the chain, you
could also use stacktrace in the exception to examine any possible breaks in
reference.

HTH
VJ
 
G

Guest

There's an AssemblyResolve event off of the AppDomain class that could help...

Something like:
AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(CurrentDomain_AssemblyResolve);

then:

private Assembly CurrentDomain_AssemblyResolve(object sender,
ResolveEventArgs args)
{
return Assembly.LoadFrom(fileName);

}

Granted you'll probably need some logic to locate/load the appropriate
library from the event handler method.

Vijay said:
To my knowledge no.. But what you can do is handle the missing assembly
exception and try to locate the assembly, copy it to the path, and start
your code. Also remember that .. the search path, current executable path,
GAC and so on.. You can alter this with App.Config / make it search in a
specific path for your dll's. If you still have the assembly in the same
path and getting the error.. , you might want to check the complete
reference hierarchy out.. There must be something broken in the chain, you
could also use stacktrace in the exception to examine any possible breaks in
reference.

HTH
VJ


jnick said:
I have the predicament of having to load several assemblies on the fly and
when I do so, I get an exception stating that one of the referenced
assemblies cannot be found. Is there any way to recurse into an assembly
to
discover the referenced assemblies without actually loading the root
assembly
where I start the load?
For example:

assembly 1 contains references to assembly 1_1 and assembly 1_2
assembly 1_1 contains reference to assembly 2
assembly 1_2 contains reference to assembly 3

If I attempt to load assembly 1 in order to get the AssemblyNames[] of
it's
referenced assemblies, I get an exception on the Load() call because the
referenced assemblies cannot be found. I have them all in the same
directory,
by the way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top