Ive built a plugin architecture in C#.NET that dynamically loads DLLs from a predefined physical file path. I am aware of assemblies possibly existing in memory location in two locations thus making it unreliable to verify a member (interface) in the assembly using something like...
if(plugin is T)
// cache the assembly
... so currently I am using the interface name to compare, then activating an instance from that. But, this has limitations too because the interface 'IPlugin' is a very common interface name which lots of third party assemblies use, (i.e. log4net, etc)
Take the following code (which is does not work):
foreach (Type t in assembly.GetTypes())
{
type = t;
if (!type.IsInterface)
{
var plugin = type.GetInterface(typeof(T).Name);
if (plugin != null)
if (plugin is T)
{
T p = (T)Activator.CreateInstance(type);
if (!this.Plugins.Select(sp => sp.Name).Contains(p.Name))
this.Plugins.Add(p);
}
}
}
My question: What is the best (reliable) way to verify the dynamically loaded DLL matches the IPlugin interface?
One thought is to hard code the IPlugin's public key token and verify that, but I am wondering if there is a more formal way. As an example, I can imagine a potential security hole with assemblies spoofing the IPlugin name, or public key token... so perhaps there is a good way to test that the DLL loaded matches the signature of the assembly loading it.
Please let me know if more clarity is required.
Very grateful!