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?

- 98,904
- 14
- 127
- 179

- 8,931
- 15
- 66
- 106
-
2The CLR is already quite capable of detecting this. And loading only the ones that are needed. You don't have to help. Helping can in fact be harmful. – Hans Passant Feb 02 '11 at 20:45
-
Not when loading assemblies dynamically from files. – Vercas Feb 02 '11 at 21:04
3 Answers
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.

- 50,833
- 6
- 93
- 125
-
Obviously, I knew how to load assemblies, but will they just WORK without holding them in a variable? – Vercas Feb 02 '11 at 20:30
-
Assembly.Load will load them into the current AppDomain, which you will need to do if you are going to use any types or methods from them. – Chris Shain Feb 02 '11 at 20:31
-
-
-
-
-
-
You can get the referenced assemblies for an assembly with the Assembly.GetReferencedAssemblies method.

- 57,867
- 14
- 111
- 145
-
Cool, but how do I load them so the assembly `X` can use them, without holding them in a variable? – Vercas Feb 02 '11 at 20:23
-
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.

- 1
- 1

- 98,904
- 14
- 127
- 179
-
I am confused by your answer. I am loading the assembly dynamically from a folder in my application's folder. – Vercas Feb 02 '11 at 21:06
-
So you've followed debugging blog and don't even know where CLR searched for this assambly? – Alexei Levenkov Feb 02 '11 at 21:24