title: Loading Byte Array Assembly
body: <p>I'm experimenting with loading an assembly using just byte arrays, but I can't figure out how to get it to work properly. Here is the setup:</p>
  
  <pre><code>public static void Main() 
  {
      PermissionSet permissions = new PermissionSet(PermissionState.None);
      AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory };
      AppDomain friendlyDomain = AppDomain.CreateDomain("Friendly", null, setup, permissions);
  
      Byte[] primary = File.ReadAllBytes("Primary.dll_");
      Byte[] dependency = File.ReadAllBytes("Dependency.dll_");
  
      // Crashes here saying it can't find the file.
      friendlyDomain.Load(dependency);
  
      AppDomain.Unload(friendlyDomain);
  
      Console.WriteLine("Stand successful");
      Console.ReadLine();
  }
  </code></pre>
  
  <p>I created two mock dlls, and renamed their extension to '.dll_' intentionally so the system wouldn't be able to find the physical files. Both <code>primary</code> and <code>dependency</code> fill correctly, but when I try to call the <code>AppDomain.Load</code> method with the binary data, it comes back with:</p>
  
  <p><code>Could not load file or assembly 'Dependency, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.</code></p>
  
  <p>Why would it be searching the system for a file?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This on the other hand seems to work:</p>
  
  <pre><code>public class Program {
      public static void Main() {
          PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted);
          AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory };
          AppDomain friendlyDomain = AppDomain.CreateDomain("Friendly", null, setup, permissions);
  
          Byte[] primary = File.ReadAllBytes("Primary.dll_");
          Byte[] dependency = File.ReadAllBytes("Dependency.dll_");
  
          // Crashes here saying it can't find the file.
          // friendlyDomain.Load(primary);
  
          Stage stage = (Stage)friendlyDomain.CreateInstanceAndUnwrap(typeof(Stage).Assembly.FullName, typeof(Stage).FullName);
          stage.LoadAssembly(dependency);
  
          Console.WriteLine("Stand successful");
          Console.ReadLine();
      }
  
  }
  
  public class Stage : MarshalByRefObject {
      public void LoadAssembly(Byte[] data) {
          Assembly.Load(data);
      }
  }
  </code></pre>
  
  <p>So it appears there is a difference between <code>AppDomain.Load</code> and <code>Assembly.Load</code>.</p>