I'm trying to load third party assemblies
dynamically to the project and use reflection
to create instance of their types.
I used:
Assembly.LoadFrom("Assembly1.dll")
Assembly.LoadFrom("Assembly2.dll")
Assembly.LoadFrom("Assembly3.dll")
Also, tried:
AppDomain.CurrentDomain.Load("Assembly1.dll")
AppDomain.CurrentDomain.Load("Assembly2.dll")
AppDomain.CurrentDomain.Load("Assembly3.dll")
However, I keep getting The method is not implemented
exception while I try to create instance of one of their type as follow:
Assembly.LoadFrom("Assembly1.dll")
Assembly.LoadFrom("Assembly2.dll")
Assembly assembly= Assembly.LoadFrom("Assembly3.dll")
Type type=assembly.GetType("Assembly3.Class1")
object instance=Activator.CreateInstance(type); //throws exception at this point
However, if I directly add reference
to Assembly1, Assembly2 and Assembly3 in the project and do:
Assembly3.Class1 testClass=new Assembly3.Class1();
I get no exception
I just wanted to know what I'm doing wrong? How do I load assemblies to the project dynamically. I'm guessing since creation of Class1
instance depends on another assembly Assembly1
and Assembly2
, so it's failing. So, how do I load all the dependent assemblies dynamically to the appdomain/loadcontext
.
Greatly appreciate your answers.
For resolve dependencies you need to handle AppDomain.AssemblyResolve Event
The MyAssemblyResolveHandler triggered for each assembly than not loaded, including dependencies.