title: Why doesn't Powershell see that it already loaded this assembly?
body: <p>This is a really long one, so hang tight. </p>
  
  <p>The idea is that an application, when referenced to Core.dll (see below), will create a new object of type <code>Core</code>, and in doing so, the constructor will scan the <code>C:\Problems</code> folder for any DLLs which contain a type that uses the <code>IPlugin</code> interface.</p>
  
  <p>It works when I reference to Core.dll from a console application. It does not work when I do the equivalent from PowerShell, unless Core.dll is in the GAC.</p>
  
  <p>These are the details:</p>
  
  <p>I have an assembly (Core.dll) which is the result of:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Core
  {
      List&lt;IPlugin&gt; plugins = new List&lt;IPlugin&gt;();
  
      public Core(string path)
      {
          LoadPlugins(path);
          foreach (IPlugin plugin in plugins)
              plugin.Work();
      }
  
      void LoadPlugins(string path)
      {
          foreach (string file in Directory.GetFiles(path))
          {
              FileInfo fileInfo = new FileInfo(file);
              Assembly dllAssembly = Assembly.LoadFile(file);
              foreach (Type type in dllAssembly.GetTypes())
              {
                  Type typeInterface = type.GetInterface("Problems.IPlugin");
                  if (typeInterface != null)
                      plugins.Add((IPlugin)Activator.CreateInstance(
                                      dllAssembly.GetType(type.ToString())));
              }
          }
      }
  }
  
  public interface IPlugin
  {
      string Name {get; set;}
      void Work();
  }
  </code></pre>
  
  <p>Then, I have a separate assembly, MyPlugin.dll, that contains the following:</p>
  
  <pre><code>public class MyPlugin : IPlugin
  {
      public string Name { get; set; }
      public void Work()
      {
          Console.WriteLine("Hello, World. I'm a plugin!");
      }
  }
  </code></pre>
  
  <p>I tested this all with a third project, TestApp.exe (with Core.dll as a reference):</p>
  
  <pre><code>static void Main(string[] args)
  {
      Core core = new Core(@"C:\Problems");
      Console.ReadLine();
  }
  </code></pre>
  
  <p>I put both Core.dll and MyPlugin.dll into C:\Problems, and TestApp.exe works like a charm! So, I write this PowerShell script:</p>
  
  <pre class="lang-none prettyprint-override"><code>Add-Type -Path "C:\Problems\Core.dll"
  $core = New-Object Problems.Core "C:\Problems"
  </code></pre>
  
  <p>This is my result (imagine a bunch of red):
  </p>
  
  <pre><code>New-Object : Exception calling ".ctor" with "1" argument(s): "Unable to load one or more o
  f the requested types. Retrieve the LoaderExceptions property for more information."
  At line:1 char:11
  + New-Object &lt;&lt;&lt;&lt;  Problems.Core "C:\Problems"
      + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
      + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
  </code></pre>
  
  <p>Drilling down into the Exception, I find this:
  </p>
  
  <pre><code>PS C:\Problems&gt; $Error[0].Exception.InnerException.LoaderExceptions
  Could not load file or assembly 'Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  </code></pre>
  
  <p>My conclusion so far is that when I'm running GetTypes() as part of the plugin loader, it's seeing that MyPlugin inherits from Problems.IPlugin, which it doesn't know where to find. But, didn't I already load it in Core.dll?</p>
  
  <p>The only way I can get around this is to sign the Core.dll assembly and add it to the GAC, but that is both annoying and does not fit my purposes.</p>
  
  <p>(All classes are in the Problems namespace, and I omitted a ton of cleanup and filtering logic to illustrate the problem.)</p>