title: How to force CSharpCodeProvider to compile for a specific target framework?
body: <p>I've got a solution which contains c# projects, some <code>netstandard</code> 2.0 and others <code>.net4.7</code>. The startup project is of course <code>net47</code>.</p>
  
  <p>At one point, the project creates code using <code>CodeDom</code> and compiles it with <code>CSharpCodeProvider</code>. The problems is that on some machines, it tries to compile the assembly for .netstandard and it fails. The failure is expected: the generated assembly references <code>EF</code> which in only available for full <code>.net framework</code>.</p>
  
  <p>How can I force <code>CSharpCodeProvider</code> to compile against <code>.net47</code>?</p>
  
  <pre><code>public bool GenerateAssembly(
          CodeDomBusinessCode compileUnit
          , string fileName
          , string assembliesPath
          , out IEnumerable&lt;string&gt; errors)
      {
          var provider = new CSharpCodeProvider();
          var parameters = new CompilerParameters
          {
              GenerateExecutable = false,
              OutputAssembly = fileName,
              GenerateInMemory = false
          };
          parameters.ReferencedAssemblies.Add("System.dll");
          parameters.ReferencedAssemblies.Add("System.Runtime.dll");
          parameters.ReferencedAssemblies.Add("System.Core.dll");
          parameters.ReferencedAssemblies.Add("System.ComponentModel.Composition.dll");
          parameters.ReferencedAssemblies.Add(Path.Combine(assembliesPath, "EntityFramework.dll"));
          parameters.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");
          parameters.ReferencedAssemblies.Add(Path.Combine(assembliesPath, "GlobalE.Server.Contracts.dll"));
  
          var results = provider.CompileAssemblyFromDom(parameters, compileUnit.Code);
          if (results.Errors.Count &gt; 0)
          {
              errors = results.Errors.OfType&lt;CompilerError&gt;().Select(x =&gt; x.ToString());
              return false;
          }
          errors = null;
          return true;
      }
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>error CS0012: The type 'System.IDisposable' is defined in an assembly
   that is not referenced. You must add a reference to assembly 
  'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
  </code></pre>
  
  <p>UPDATE:
  If I change all projects to <code>net47</code> (so that there is no <code>netstandard</code> project in the solution), the error will disappear, but I want to keep as many projects on <code>netstandard</code> as possible.</p>