Consider the following code in a DLL:
public class ReceivingClass
{
private Assembly myAssembly;
private Type typeOfClass;
public ReceivingClass()
{
myAssembly = Assembly.LoadFile(@"E:\VSProjects\TestDynamicLinking\MyLib\bin\Debug\MyLib.dll");
//Can I use this type somehow to resolve the type in the below method?
typeOfClass = myAssembly.GetType("ExportedClass");
}
public bool ReceiveMethod(ExportedClass classobj)
{
return true;
}
}
So, the problem is that in the ReceiveMethod above, ExportedClass is a class which is defined in the Assembly that I dynamically loaded in the Constructor. So, can I somehow resolve the type of ExportedClass such that I don’t have to use dynamic?
Some choices you have are either
I would usually use the second approach. Just define an interface that ExportedClass implements in another (shared) assembly. Then you can try to cast the created instance to that interface. like that:
Of course, that is assuming that you have control over the referenced library. Or if you are defining a plugin infrastructure or something like that, you should include that as a requirement.