title: Missing assembly references in dynamically compiled code
body: <p>Thanks for the messages about my first post about this problem. I will do a repost and try to be more clear this time. I guess this may be a trivial problem but I'm really stuck and need some help. This is my first time posting here.</p>
  
  <pre><code>using System;
  using System.CodeDom.Compiler;
  using System.Reflection;
  using Microsoft.CSharp;
  namespace DynamicCode
  {
  public class DynaCore
  {
  string WorkingCode = 
      "using System;"+
      "using System.Collections.Generic;"+
      "namespace DynaCore"+
      "{"+
      "   public class DynaCore"+
      "   {"+
      "       static public string DynamicResult()"+
      "       {"+
      "           return \"I'm compiled\";"+
      "       }"+
      "   }"+
      "}";
  
  
  string PredicateTemplCode = 
      "using System;"+
      "using System.Linq;"+
      "using System.Collections.Generic;"+
      "namespace DynaCore"+
      "{"+
      "   public class DynaCore"+
      "   {"+
      "       static public Func&lt;{1}, bool&gt; DynamicResult()"+
      "       {"+
      "           return new Func&lt;{1}, bool&gt;({2});"+
      "       }"+
      "   }"+
      "}";
  
  public DynaCore()
  {
      string compiledString = WorkingCompilation(WorkingCode);
      Func&lt;bool, bool&gt; compiladePredicate = NotWorkingCompilation&lt;bool&gt;(PredicateTemplCode, "(o)=&gt; true");
  }
  
  string WorkingCompilation(string code)
  {
      var cParams = new CompilerParameters();
      cParams.GenerateInMemory = true;
      cParams.TreatWarningsAsErrors = false;
      cParams.GenerateExecutable = false;
      cParams.CompilerOptions = "/optimize /target:library";
  
      var curAss = Assembly.GetExecutingAssembly();
  
      cParams.ReferencedAssemblies.Add("System.dll");
      cParams.ReferencedAssemblies.Add("mscorlib.dll");
      cParams.ReferencedAssemblies.Add("System.dll");
      cParams.ReferencedAssemblies.Add("System.Data.dll");
      cParams.ReferencedAssemblies.Add(curAss.Location);
  
      var provider = new CSharpCodeProvider();
  
      var compalerResult = provider.CompileAssemblyFromSource(cParams, code);
  
      if (compalerResult.Errors.HasErrors)
      {
          var complieError = "";
          foreach (CompilerError ce in compalerResult.Errors)
              complieError += ce + " ";
  
          throw new Exception(complieError.Trim());
      }
  
      Module module = compalerResult.CompiledAssembly.GetModules()[0];
      Type mt = null;
      MethodInfo methInfo = null;
  
      return (string)module.GetType("DynaCore.DynaCore").GetMethod("DynamicResult").Invoke(null, null);
  }
  
  
  Func&lt;T, bool&gt; NotWorkingCompilation&lt;T&gt;(string code, string predicateString)
  {
      var cParams = new CompilerParameters();
      cParams.GenerateInMemory = true;
      cParams.TreatWarningsAsErrors = false;
      cParams.GenerateExecutable = false;
      cParams.CompilerOptions = "/optimize /target:library";
  
      var curAss = Assembly.GetExecutingAssembly();
  
      cParams.ReferencedAssemblies.Add("System.dll");
      cParams.ReferencedAssemblies.Add("mscorlib.dll");
      cParams.ReferencedAssemblies.Add("System.dll");
      cParams.ReferencedAssemblies.Add("System.Data.dll");
      cParams.ReferencedAssemblies.Add("System.Core.dll");
      cParams.ReferencedAssemblies.Add(curAss.Location);
  
      var provider = new CSharpCodeProvider();
  
      var codeToRun = code.Replace("{1}", typeof(T).Name).Replace("{2}", predicateString);
      var compalerResult = provider.CompileAssemblyFromSource(cParams, codeToRun);
  
      if (compalerResult.Errors.HasErrors)
      {
          var complieError = "";
          foreach (CompilerError ce in compalerResult.Errors)
              complieError += ce + " ";
  
          throw new Exception(complieError.Trim());
      }
  
      Module module = compalerResult.CompiledAssembly.GetModules()[0];
      Type mt = null;
      MethodInfo methInfo = null;
  
      return (Func&lt;T, bool&gt;)module.GetType("DynaCore.DynaCore").GetMethod("DynamicResult").Invoke(null, null);
  }
  }
  }
  </code></pre>
  
  <p>The problem is that when I reference <strong>System.Core.dll</strong> in <strong>ReferencedAssemblies.Add("System.Core.dll")</strong>, it gives me a compiler error: </p>
  
  <p><strong>error CS0006: Metadata file 'System.Core.dll' could not be found</strong></p>
  
  <p>I'm using v3.5 and VS 2008. </p>