title: How to access class with interface from DLL?
body: <p>I'm developing a program that uses plugins. All plugins are classes that inherits from KrotAPI.IKrotAPI interface. The interface is stored in krotapi.cs file, that is common for both host's csproj and every plugins' *.csproj.</p>
  
  <p>The host loads plugins using this code</p>
  
  <pre><code>dynamic LoadPlugin(Assembly asm)
  {
    foreach (Type type in asm.GetTypes())
    {
      if (type.GetInterface("KrotAPI.IKrotPlugin") != null)
      {
        PluginType = type;
        dynamic inst = Activator.CreateInstance(type);
        KrotAPI.IKrotPlugin inst2 = inst as KrotAPI.IKrotPlugin;
        if (inst2 == null) return inst;
        Console.WriteLine("Link to API is set up.");
        return inst2;
      }
    }
    throw new Exception("There are no valid plugin(s) in the DLL.");
  }
  </code></pre>
  
  <p>The <code>inst2</code> is always null, and I am forced to use slow and buggy dynamic calls. How to make it like <code>inst</code>, but with <code>KrotAPI.IKrotPlugin</code> type?</p>
  
  <hr>
  
  <p>Second question on almost same topic.
  Some plugins' functions returns an result of type KrotAPI.FindData (it's an structure, that is stored in above krotapi.cs file). But i cannot access it:</p>
  
  <pre><code>dynamic fd = new KrotAPI.FindData();
  if (NextItemInfo != null) //NextItemInfo is an instance of that struct
      fd = NextItemInfo;
  listBox1.Items.Add(fd.FileName);
  </code></pre>
  
  <p>On last row NET Framework throws this exception: </p>
  
  <blockquote>
    <p>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
    'System.ValueType' does not contain a definition for 'FileName'</p>
  </blockquote>
  
  <p>however the FileName field is hardcoded in the struct and certainly not a null, or</p>
  
  <blockquote>
    <p>Microsoft.CSharp.RuntimeBinder.RuntimeBinderInternalCompilerException
    An unexpected exception occurred while binding a dynamic operation</p>
  </blockquote>
  
  <p>if I replace the last line with <code>KrotAPI.FindData fd2 = (KrotAPI.FindData) fd; listBox1.Items.Add(fd2.FileName);</code>.</p>
  
  <p>WTF?</p>