title: Dynamically loading assemblies for a plugin architecture
body: <p>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...</p>
  
  <pre><code>if(plugin is T)
      // cache the assembly
  </code></pre>
  
  <p>... 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)</p>
  
  <p>Take the following code (which is does not work):</p>
  
  <pre><code>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 =&gt; sp.Name).Contains(p.Name))
                      this.Plugins.Add(p);
              }
      }
  }
  </code></pre>
  
  <p>My question: What is the best (reliable) way to verify the dynamically loaded DLL matches the IPlugin interface?</p>
  
  <p>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.</p>
  
  <p>Please let me know if more clarity is required.</p>
  
  <p>Very grateful!</p>