I have a plugin system in c# originated from stackoverflow. The key parts are Assembly.LoadFile, Type.IsAssignableFrom and Activator.CreateInstance. It works, however I don't fully understand how IsAssignableFrom can identify the types load from outer assemblies. Say, I have
public interface PluginInterface
{
int calculateSomething();
}
I've compiled it into PluginInterface.dll.
PluginInterface.dll is a reference in an example plugin, eg.:
public class CalculateOnePlugin : PluginInterface
{
public int calculateSomething() { return 1; }
}
It is compiled into CalculateOnePlugin.dll.
Now, create an application with reference to PluginInterface.dll and try to load and use CalculateOnePlugin.dll:
var pluginAssembly = Assembly.LoadFile("CalculateOnePlugin.dll");
Type interfaceType = typeof(PluginInterface);
Type[] typesInLoadedAssembly = pluginAssembly.GetTypes();
Type pluginType = null;
foreach (var typeInLoadedAssembly in typesInLoadedAssembly)
if (interfaceType.IsAssignableFrom(typeInLoadedAssembly)) // ???
{
pluginType = typeInLoadedAssembly;
break;
}
if (pluginType!=null)
{
PluginInterface pi = (PluginInterface)Activator.CreateInstance(pluginType);
// and do what you want with that plugin object
}
My question is how IsAssignableFrom identifies and matches the types loaded from two external assembly? What assembly or class properties are taken into consideration by IsAssignableFrom?
If I copy the source of PluginInterface and compile it into (eg.) AnotherPluginInterface.dll and use this reference when building CalculateOnePlugin.dll then it won't work. So type matching not just cares about method signatures, for example, it knows somehow the origin of the assembly and the type.
When I use two different version of PluginInterface for the application and the plugin (let's say a method was added to the latter) it won't work again, so the origin of the two dlls is not enough, it must have some version stuff inside it.
But simply recompiling PluginInterface between the two usage (app and plugin) does not make mess, it works.
Is the GUID of PluginInterface.dll included into references of CalculateOnePlugin.dll and this is how IsAssignableFrom knows that they're the same? Or a hash-like value is used for types? Or both of them?