Get assembly's requireed assemblies?

244 views Asked by At

I am loading an assembly X.dll in my program, where X.dll can be anything, and I create an instance of the class X.A_Class. But what if the assembly X requires assemblies A, B, C and D?
How do I detect this?
How do I load them without holding them in a variable?

3

There are 3 answers

8
Chris Shain On BEST ANSWER

You can use Assembly.GetReferencedAssemblies as @alexn mentioned, and then use Assembly.Load to load them. Alternatively, you can hook AppDomain.CurrentDomain.AssemblyResolve and load them on demand.

If you do need to iterate them, make sure you do it recursively to get transitive dependencies.

2
alexn On

You can get the referenced assemblies for an assembly with the Assembly.GetReferencedAssemblies method.

2
Alexei Levenkov On

Refferenced assemblies normally will be loaded automatically (see related messages like How is an assembly resolved in .NET? for starting links).

If you are loading assemblies not from standard locations (like GAC and application's root folder) you may need either setup path to load referenced assemblies from (search for "assembly default load path" - i.e. app config file - http://msdn.microsoft.com/en-us/library/823z9h8w.aspx) or load them yourself from AssemblyReslove event as mentioned in other answers.

The best way to start debugging the issues with loading assemblies is read blogs at: http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57120.aspx (and related posts http://blogs.msdn.com/b/suzcook/archive/tags/loader+debugging+advice/ )

EDIT: folder -> "root folder" + link to config file topic for probing paths.