I need to create a class instance from a specific assembly. The assemblies (and it’s dependencies) must be loadedfrom assemblies/dlls that are known runtime, not build time. So Nuget is not an option.
I can load the assemblies with it’s dependencies. No problem. However one dependency also exists as another dependency of a Nuget package. However as an older version.
When I create an instance using the manually loaded assemblies, it does not use the manually loaded dependency, but the one supplied by the Nuget package. This is an older version and results in a wrong instance.
To clarify an example:
class Child : Parent
Class Child
resides in assembly A; class Parent
resides in assembly B-version2.
Another class in a referenced Nuget package references resides in assembly B-version1. These assemblies are placed in the application root.
I load assemblies A and B-version2 manually. Assembly B-version1 is loaded implicitly. Both assemblies occur in the Debug-Modules view.
When I create a Child instance using reflection, the assembly B-version1 apparently precedes over B-version2. Probably because v1 is available in the application root.
As a result the Child
inhertits from the Parent
in assembly version 1. This results in the Child
instance missing properties that were added in version 2.
How can I create a Child
using assembly B-version2 with the correct Parent
?
Failed attempt:
I tried to hookup to the AppDomain.CurrentDomain.AssemblyLoad
and AppDomain.CurrentDomain.AssemblyResolve
events.
On load I store the manually laoded assembly. On resolve I return “my” loaded version.
As AssemblyResolve
only fires when the runtime cannot resolve the assembly another way, AssemblyResolve
will not be called as the runtime can resolves the assembly by simply using the wrong one in the application root.