Calax
Calax

Reputation: 75

Dynamic loading dll that includes references on external libraries

I load dll (dll_1) dynamically and run the code from it. The problem occurs when this (dll_1) uses another dll (dll_2). I can't embed dll_2 in dll_1.

I load dll by using Assembly.LoadFile then CreateInstance and InvokeMember.

What should I do?

Upvotes: 0

Views: 1970

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100630

Easiest option is not to use LoadFile and use Load instead as it resolves assemblies the same way as normal assembly resolution works.

Next option is preload dependencies or use AssemblyResolveEvent as Eric J suggested. If you go this route please read Suzanne Cook's articles on loading assemblies and binding context (note that there are more related article in the same blog).

Except from the linked article:

LoadFrom advantages:

Assemblies can be loaded from multiple paths, not just from beneath the ApplicationBase.

Dependencies already loaded in this context will automatically be found.

Dependencies in the same dir as the requesting LoadFrom context assembly will automatically be found.

Upvotes: 0

Eric J.
Eric J.

Reputation: 150238

If I understand your question correctly, you want to detect when a reference to an assembly is not being resolved and be able to intercede to ensure that the assembly is correctly loaded.

You can use the AppDomain.AssemblyResolveEvent to do just that.

Upvotes: 2

Related Questions