title: Dynamically loaded Assembly not loading in new AppDomain
body: <p>This is not a duplicate - I have reviewed this related StackOverflow question with no luck: <a href="https://stackoverflow.com/questions/658498/how-to-load-assembly-to-appdomain-with-all-references-recursively">How to Load an Assembly to AppDomain with all references recursively?</a></p>
  
  <p>I have two console applications.  AssemblyLoaderTest.exe and testapp.exe</p>
  
  <ol>
  <li>I am trying to use AssemblyLoaderTest.exe to dynamically load testapp.exe and call a method from a class within testapp.exe</li>
  <li>So far the code works - the method "TestWrite()" in testapp.exe is executed correctly (and outputsuccess.txt is written), <em>however</em>, <strong>testapp.exe is loaded in the same AppDomain</strong>, which is proven because "CallMethodFromDllInNewAppDomain" always returns false.  I am trying to load testapp.exe in a <strong>new AppDomain</strong>. </li>
  </ol>
  
  <p>My question: how can I modify the below code so that testapp.exe is loaded in a new AppDomain, and as a result, "CallMethodFromDllInNewAppDomain" returns true?  Thank you!</p>
  
  <p>Code below.  Both can be simply copied into new Console applications in VS and executed/compiled.</p>
  
  <p>Console application #1:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Reflection;
  using System.Security.Policy;
  
  namespace AssemblyLoaderTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              List&lt;object&gt; parameters = new List&lt;object&gt;();
              parameters.Add("Test from console app");
              bool loadedInNewAppDomain = DynamicAssemblyLoader.CallMethodFromDllInNewAppDomain(@"c:\temp\testapp.exe", "testapp.TestClass", "TestWrite", parameters);
          }
      }
      public static class DynamicAssemblyLoader
      {
          public static string ExeLoc = "";
          public static bool CallMethodFromDllInNewAppDomain(string exePath, string fullyQualifiedClassName, string methodName, List&lt;object&gt; parameters)
          {
              ExeLoc = exePath;
              List&lt;Assembly&gt; assembliesLoadedBefore = AppDomain.CurrentDomain.GetAssemblies().ToList&lt;Assembly&gt;();
              int assemblyCountBefore = assembliesLoadedBefore.Count;
              AppDomainSetup domaininfo = new AppDomainSetup();
              Evidence adevidence = AppDomain.CurrentDomain.Evidence;
              AppDomain domain = AppDomain.CreateDomain("testDomain", adevidence, domaininfo);
              AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
              domain.CreateInstanceFromAndUnwrap(exePath, fullyQualifiedClassName);
              List&lt;Assembly&gt; assemblies = domain.GetAssemblies().ToList&lt;Assembly&gt;();
              string mainExeName = System.IO.Path.GetFileNameWithoutExtension(exePath);
              Assembly assembly = assemblies.FirstOrDefault(c =&gt; c.FullName.StartsWith(mainExeName));
              Type type2 = assembly.GetType(fullyQualifiedClassName);
              List&lt;Type&gt; parameterTypes = new List&lt;Type&gt;();
              foreach (var parameter in parameters)
              {
                  parameterTypes.Add(parameter.GetType());
              }
              var methodInfo = type2.GetMethod(methodName, parameterTypes.ToArray());
              var testClass = Activator.CreateInstance(type2);
              object returnValue = methodInfo.Invoke(testClass, parameters.ToArray());
              List&lt;Assembly&gt; assembliesLoadedAfter = AppDomain.CurrentDomain.GetAssemblies().ToList&lt;Assembly&gt;();
              int assemblyCountAfter = assembliesLoadedAfter.Count;
              if (assemblyCountAfter &gt; assemblyCountBefore)
              {
                  //  Code always comes here
                  return false;
              }
              else
              {
                  // This would prove the assembly was loaded in a NEW domain.  Never gets here.
                  return true;
              }
          }
          public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
          {
              // This is required I've found
              return System.Reflection.Assembly.LoadFrom(ExeLoc);
          }
      }
  }
  </code></pre>
  
  <p>Console application #2:</p>
  
  <pre><code>using System;
  namespace testapp
  {
      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine("Hello from console");
          }
      }
      [Serializable]
      public class TestClass : MarshalByRefObject
      {
          public void TestWrite(string message)
          {
              System.IO.File.WriteAllText(@"outputsuccess.txt", message);
          }
      }
  
  }
  </code></pre>