title: Is it possible to load an assembly targeting a different .NET runtime version in a new app domain?
body: <p>I've an application that is based on .NET 2 runtime.  I want to add a little bit of support for .NET 4 but don't want to (in the short term), convert the whole application (which is very large) to target .NET 4.</p>
  
  <p>I tried the 'obvious' approach of creating an application .config file, having this:</p>
  
  <pre><code>&lt;startup useLegacyV2RuntimeActivationPolicy="true"&gt;
    &lt;supportedRuntime version="v4.0" /&gt;
  &lt;/startup&gt;
  </code></pre>
  
  <p>but I ran into some problems that I noted <a href="https://stackoverflow.com/questions/2520104/problem-using-uselegacyv2runtimeactivationpolicy-supportedruntime-in-an-appli">here</a>.</p>
  
  <p>I got the idea of creating a separate app domain.  To test it, I created a WinForm project targeting .NET 2.  I then created a class library targeting .NET 4.  In my WinForm project, I added the following code:</p>
  
  <pre><code>        AppDomainSetup setup = new AppDomainSetup();
          setup.ApplicationBase = "path to .NET 4 assembly";
          setup.ConfigurationFile = System.Environment.CurrentDirectory + 
            "\\DotNet4AppDomain.exe.config";
  
          // Set up the Evidence
          Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
          Evidence evidence = new Evidence(baseEvidence);
  
          // Create the AppDomain      
          AppDomain dotNet4AppDomain = AppDomain.CreateDomain("DotNet4AppDomain", evidence, setup);
          try
          {
              Assembly doNet4Assembly = dotNet4AppDomain.Load(
                 new AssemblyName("MyDotNet4Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=66f0dac1b575e793"));
              MessageBox.Show(doNet4Assembly.FullName);
          }
          finally
          {
              AppDomain.Unload(dotNet4AppDomain);
          }
  </code></pre>
  
  <p>My DotNet4AppDomain.exe.config file looks like this:</p>
  
  <pre><code>&lt;startup useLegacyV2RuntimeActivationPolicy="true"&gt;
    &lt;supportedRuntime version="v4.0" /&gt;
  &lt;/startup&gt;
  </code></pre>
  
  <p>Unfortunately, this throws the BadImageFormatException when dotNet4AppDomain.Load is executed.  Am I doing something wrong in my code, or is what I'm trying to do just not going to work?</p>
  
  <p>Thank you!</p>