我在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);
}
}
}
}
根据[this](http://permalink.gmane.org/gmane.comp.gnome.mono.patches/178509),旧的评估者是静态的,新的评估者是成员。我将不得不检查我的dll版本。 – kristianp 2012-04-26 07:51:19