title: Calling Activator.Createinstance(...) with DLL that has static variable
body: <p>I have a DLL, (<strong>that I cannot change</strong>!), that has static variable.
  I want to call <code>Activator.CreateInstance(...)</code> but create a brand new Instance rather than share a previously loaded Instance.</p>
  
  <pre><code>// ClassLibrary.dll
  namespace ClassLibrary
  {
    public class Foo
    {
      private static int Number { get; set; } // This is a static member...
      public Foo()
      {
        Number++;
      }
  
      public int Bar()
      {
        return Number;
      }
    }
  }
  
  // ConsoleApplication.exe
  static int InvokeMethod()
  {
    var dir = Directory.GetCurrentDirectory();
    var path = Path.Combine(dir, "ClassLibrary.dll");
  
    var asm = Assembly.LoadFile(path);
    var type = asm.GetType("ClassLibrary.Foo");
    var instance = Activator.CreateInstance(type, new object[] { });
    var method = type.GetMethod("Bar");
    return (int)method.Invoke(instance, null);
  }
  
  private static void Main(string[] args)
  {
    var val1 = InvokeMethod(); // 1
    var val2 = InvokeMethod(); // 2! I want it to be 1
  }
  </code></pre>
  
  <p>I have tried the same with <code>AppDomain.Load()</code>, but the static value is still being shared.</p>
  
  <p>Any suggestions on how I can load a brand new instance rather than sharing the previously loaded dll.</p>
  
  <p><strong>Edit 1</strong>: Here is the code of the AppDomain load, but the result is the same.</p>
  
  <pre><code>static int InvokeMethodDomain()
  {
    var dir = Directory.GetCurrentDirectory();
    var path = Path.Combine(dir, "ClassLibrary.dll");
  
    var dom = AppDomain.CreateDomain(Guid.NewGuid().ToString());
    try
    {
      var asn = new AssemblyName {CodeBase = path};
      var asm = dom.Load(asn);
      var type = asm.GetType("ClassLibrary.Foo");
      var instance = Activator.CreateInstance(type, new object[] { });
      var method = type.GetMethod("Bar");
      return (int) method.Invoke(instance, null);
    }
    finally
    {
      AppDomain.Unload(dom);
    }
  }
  </code></pre>