title: Mono Compiler as a Service (MCS)
body: <p>I'd like to consume Mono's compiler as a service from my regular .NET 3.5 application.</p>
  
  <p>I've downloaded the latest bits (2.6.7), created a simple console application in Visual Studio and referenced the Mono.CSharp dll.</p>
  
  <p>Then, in my console app (straight out of a sample online):</p>
  
  <pre><code>    Evaluator.Run("using System; using System.Linq;");
      bool ress;
      object res;
      Evaluator.Evaluate(
           "from x in System.IO.Directory.GetFiles (\"C:\\\") select x;",
           out res, out ress);
  
      foreach (var v in (IEnumerable)res)
      {
          Console.Write(v);
          Console.Write(' ');
      }
  </code></pre>
  
  <p>This throws an exception at Evaluator.Run (the first line): </p>
  
  <pre><code>Illegal enum value: 2049.
  Parameter name: access
  </code></pre>
  
  <p>This is because the dll was compiled using Mono.exe, not csc.exe, I believe.</p>
  
  <p>I've tried downloading the Mono.CSharp dll directly from <a href="http://tirania.org/blog/archive/2010/Apr-27.html" rel="nofollow noreferrer">http://tirania.org/blog/archive/2010/Apr-27.html</a> in the demo-repl.zip file...and that does not throw an exception...However the out parameter (res) after calling Evaluator.Evaluate is null...so I'm not sure what's going wrong. No exception is thrown...</p>
  
  <p>So, I'd like to figure out why the dll I downloaded from the demo-repl.zip returns null. </p>
  
  <p>EDIT: I figured out why it returns null. It seems like for some reason the compiler isn't picking up the System.Linq namespace...though I can't tell why...If I just Evaluate "System.IO.Directory.GetFiles (\"C:\\")", it works fine.</p>
  
  <p>UPDATE: It definitely seems like there's something wrong with the Mono compiler picking up referenced System assemblies. If I directly copy the sample of their csharp console tool:</p>
  
  <pre><code>csharp&gt; var list = new int [] {1,2,3};
  csharp&gt; var b = from x in list
     &gt;    where x &gt; 1
     &gt;    select x;
  csharp&gt; b;
  </code></pre>
  
  <p>I get the exception:</p>
  
  <pre><code>{interactive}(1,25): error CS1935: An implementation of `Select' query expressio
  n pattern could not be found. Are you missing `System.Linq' using directive or `
  System.Core.dll' assembly reference?
  </code></pre>
  
  <p>Also, in order for the MCS to actually be a feasible solution, I'll need to modify the compiler so that it emits to one single dynamic assembly, instead of emitting one assembly per evaluate call (otherwise it presents a major memory leak, which I've dealt with before in the form of the CSharpCodeProvider). Does anyone have an idea of how difficult this will be or can anyone point me in the right direction here?</p>
  
  <p>Thanks.</p>