How to dynamically load assembly w/ dependencies??

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
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Jon,

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

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