2012-04-25 39 views
1

我在Ubuntu 11.10上運行單聲道2.10版本。我試圖運行http://blog.davidebbo.com/2012/02/quick-fun-with-monos-csharp-compiler-as.html上提供的示例,但似乎針對不同版本的單聲道。例如Compile是Evaluator上的靜態方法。我對他的示例做了以下更改,但沒有成功。任何人都可以提供正確的更改,並且沒有人知道是否有任何關於Mono.CSharp API更改的信息?我的編譯器報告的版本如下:版本2.10中的Mono.CSharp(編譯器即服務)更改

$ dmcs --version 
Mono C# compiler version 2.10.5.0 

我使用此命令行編譯如下代碼:

公契-r:Mono.CSharp Sample.cs

,得到了這樣的警告時,編譯。

dmcs -r:Mono.CSharp Sample.cs 
Sample.cs(26,17): warning CS0219: The variable `compiledMethod' is assigned but its value is never used 
Compilation succeeded - 1 warning(s) 

這是運行代碼的結果:

$ ./Sample.exe 
{interactive}(2,40): error CS1525: Unexpected symbol `namespace', expecting `end-of-file' or `using' 
{interactive}(4,70): error CS0101: The namespace `UserCode' already contains a definition for `Foo' 
{interactive}(4,70): (Location of the symbol related to previous error) 

這是我的代碼至今:

using System; 
using System.IO; 
using Mono.CSharp; 
using System.Reflection; 

namespace Sample 
{ 
    public interface IFoo { string Bar(string s); } 

    class Program 
    { 
     const string code = @" 
      using System; 
      namespace UserCode 
      { 
       public class Foo : Sample.IFoo 
       { 
        public string Bar(string s) { return s.ToUpper(); } 
       } 
      } 
     "; 

     static void Main(string[] args) 
     { 
      Mono.CSharp.Evaluator.Init(new string[] {}); 
      Evaluator.ReferenceAssembly(Assembly.GetExecutingAssembly()); 

      var compiledMethod = Evaluator.Compile(code); 

      for (;;) 
      { 
       string line = Console.ReadLine(); 
       if (line == null) break; 

       object result; 
       bool result_set; 
       Evaluator.Evaluate(line, out result, out result_set); 
       if (result_set) Console.WriteLine(result); 
      } 
     } 
    } 
} 
+0

根據[this](http://permalink.gmane.org/gmane.comp.gnome.mono.patches/178509),舊的評估者是靜態的,新的評估者是成員。我將不得不檢查我的dll版本。 – kristianp 2012-04-26 07:51:19

回答

3

單2.10帶有僅支持計算器表達式和語句。您的代碼包含Mono 2.10不支持的類型聲明。

Mono 2.11或git master Mono.CSharp支持類型聲明和其他高級功能。