title: Dynamically load assembly from byte[]
body: <p>I need to <strong>load several assemblies from memory multiple times at runtime</strong>. I also need a possibility to <strong>reload those assemblies</strong> after some changes made to them. </p>
  
  <p>I've tried standard <strong>Assembly.Load</strong> method and it worked fine until I realized that when I loaded the same assembly again, it appeared twice in </p>
  
  <pre><code>AppDomain.CurrentDomain.GetAssemblies()
  </code></pre>
  
  <p>which is not what I want to accomplish due to multiple load operations. I know that if I will load DLL from disk it won't load if it's there in unchanged form.</p>
  
  <p>Then I tried with loading DLLs into <strong>new AppDomain</strong>, but I got problem with dependencies.</p>
  
  <p>Here is simplified scenario:</p>
  
  <p>I have shared DLL, call it AssemblyA, which is referenced by main application.</p>
  
  <p>I have also several DLLs which I want to dynamically load (call them AssemblyB...Z). These DLLs also reference AssemblyA.</p>
  
  <p>My main application, as I mentioned above, references AssemblyA which has abstract class with method implemented by main application(call it LoadAssemblies). This method is used for loading dynamically rest of assemblies via new AppDomain. </p>
  
  <p>Here is the current code of LoadAssemblies (I tried with manually loading dependencies):</p>
  
  <pre><code>public override void LoadAssemblies()
  {
  AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain");
  
  
  foreach (byte[] xmlDLL in listOfDLLS)
  {
      Assembly reflection = Assembly.ReflectionOnlyLoad(xmlDLL);
  
  
      foreach (var dependencies in reflection.GetReferencedAssemblies())
      {
          if (dependencies.Name.Equals("AssemblyA"))
          {
              Assembly pluginr = Assembly.ReflectionOnlyLoad(AssemblyA);
                  foreach (var d in pluginr.GetReferencedAssemblies())
                      pluginDomain.Load(d);
                      pluginDomain.Load(plugin);
          }
          else
              pluginDomain.Load(dependencies);
       }        
      Assembly assembly = pluginDomain.Load(xmlDLL);  
  }
  </code></pre>
  
  <p>The code above is getting error on line</p>
  
  <pre><code>Assembly assembly = pluginDomain.Load(xmlDLL);
  </code></pre>
  
  <p>saying that it:</p>
  
  <pre><code>could not load assembly or one of its dependencies
  </code></pre>
  
  <p>Tried also with AssemblyResolver event, but it complains about Serializable, and then when I made it serializable, about type.</p>
  
  <p>PS. Unfortunately I can't save DLLs on disk.</p>