I am loading a DLL dynamically into an Assembly
. My problem is when I try to use System.Reflection
to get a certain type from that loaded assembly, that I want to use to create an instance of that type. The type I want happens to be an interface.
This is based on code that I have used successfully in the past, but for a new project, it fails. The problem seems to be that the types being compared are from two different assemblies.
Here is the code:
IMyInterface _classInstance;
String path = Path.Combine(Application.StartupPath, "mydynamic.dll");
if (File.Exists(path))
{
Byte[] ba = File.ReadAllBytes(path);
if (ba.Length > 0)
{
Assembly dll = Assembly.Load(ba);
if (dll != null)
{
foreach (Type T in dll.GetTypes())
{
if (typeof(IMyInterface).IsAssignableFrom(T))
_classInstance = (IMyInterface)Activator.CreateInstance(T);
}
}
}
}
Using the advice from this question, I was able to determine that typeof(IMyInterface).Module.FullyQualifiedName
returns the executing application (the EXE I am debugging). That, of course, does not match what I see for T.Module.FullyQualifiedName
, which is the DLL.
I would like to resolve this without injecting some go-between assembly that does nothing but provide the type, if that is possible.