How to Load assembly to AppDomain with all references recursively?

Posted by abatishchev on Stack Overflow See other posts from Stack Overflow or by abatishchev
Published on 2009-03-18T14:30:09Z Indexed on 2010/06/17 6:13 UTC
Read the original article Hit count: 225

Filed under:
|
|
|
|

I want to load to new AppDomin some assembly which has a complex references tree (MyDll.dll -> Microsoft.Office.Interop.Excel.dll -> Microsoft.Vbe.Interop.dll -> Office.dll -> stdole.dll)

As far as I understood, when an assembly is been loaded to AppDomain, it's references would not be loaded automatically, and I have to load them manually. So when I do:

string dir = @"SomePath"; // different from AppDomain.CurrentDomain.BaseDirectory
string path = System.IO.Path.Combine(dir, "MyDll.dll");

AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
setup.ApplicationBase = dir;
AppDomain domain = AppDomain.CreateDomain("SomeAppDomain", null, setup);

domain.Load(AssemblyName.GetAssemblyName(path));

and got FileNotFoundException:

Could not load file or assembly 'MyDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

I think the key word is one of its dependencies.

Ok, I do next before domain.Load(AssemblyName.GetAssemblyName(path));

foreach (AssemblyName refAsmName in Assembly.ReflectionOnlyLoadFrom(path).GetReferencedAssemblies())
{
    domain.Load(refAsmName);
}

But got FileNotFoundException again, on another (referenced) assembly.

How to load all references recursively?

Have I to create references tree before loading root assembly? How to get an assembly's references without loading it?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET