How to dynamically load assembly w/ dependencies??

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

I'm trying to access my application files for a plug-in I'm building for a
third party application.

I have an assembly with several dependency assemblies. The dependency
assemblies are in the same directory as the assembly.

d:\dev\cs\MyApp\bin\Release\MyApp.dll
d:\dev\cs\MyApp\bin\Release\Dependency1.dll
d:\dev\cs\MyApp\bin\Release\Dependency2.dll

Note that all DLLs are strongly named with a keyfile. Also note that the
project MyApp.dll references Dependency1.dll, et al, by the Project for
debugging.

I am trying to load MyApp.dll dynamically from my plug-in that runs in the
third party application.

Assembly pbAssembly =
Assembly.LoadFile(GetRegSetting("Settings", "AppPath",
AppDomain.CurrentDomain.BaseDirectory)
+ "\\MyApp.dll"); // this is something I store manually

This works fine so far. When I attempt to call the properties/methods on my
DLL, I get the error message:

"File or assembly name Dependency1.dll, or one of its dependencies, was
not found."

This makes sense because the assembly loader checks for the dependencies
from the current application's base directory and the GAC and the CLR
runtime (not in that order), and nowhere else.

I don't want to use the GAC for my application. I would rather manually
reference each file, but even then I don't know how to do that other than
loading the Assembly file and disposing it (does that work? can I do that?
would it be sufficient?).

So my question is: How can I modify or append to the PrivateBinPath for the
current AppDomain?

Thanks for any help,
Jon
 
Hi Jon,

Subscribe to the current AppDomain's AssemblyResolve event and load the
referenced assemblies manually since you know their location on disk.
 
Back
Top