title: Problems loading assemblies into a new AppDomain for static class/method invocation
body: <p>I've been having some major problems with executing .NET assemblies via C#. I want to invoke a static method (Main), from a static class. I had it working in the current domain, but apparently I can't unload the assemblies from there.</p>
  
  <p>Firstly, I tried creating a new AppDomain:</p>
  
  <pre><code>AppDomain domain = AppDomain.CreateDomain("PluginDomain");
  </code></pre>
  
  <p>Since my main issue is with resolving the assemblies I'm trying to load, I tried to hook the AssemblyResolve event:</p>
  
  <pre><code>domain.AssemblyResolve += new ResolveEventHandler(this.OnAssemblyResolve);
  </code></pre>
  
  <p>However, I get an error about serialization with this (ie. the this class).</p>
  
  <p>I have successfully loaded an assembly file which happens to be located in the BaseDirectory by simply using:</p>
  
  <pre><code>domain.Load("Assembly1");
  </code></pre>
  
  <p>But the problematic assembly is located within two subdirectories of the BaseDirectory, ie. BaseDirectory\dir1\dir2\Assembly2.dll - I get a FileNotFoundException when using domain.Load() with a string assembly name (with a PrivateBinPath setup to the correct subdirectory location) and also when using domain.Load() with the assembly bytes (from File.ReadAllBytes()).</p>
  
  <p><s>I am aware that AppDomain.Load() is deprectated, but it seemed like the only useable method to me, considering that I don't want to force the class containing a static method to be non-static and instantiatable.</s></p>
  
  <p>Anyway, what can I do to ensure the assembly file is loaded into the new AppDomain correctly?</p>
  
  <p>My main objective with this, is to dynamically execute a method from an assembly which can make calls into my own program, yet the assembly must also be unloaded completely when necessary. It's intended to be somewhat of a plugin system.</p>