Ubuntu 11.10 で Mono バージョン 2.10 を実行しています。http://blog.davidebbo.com/2012/02/quick-fun-with-monos-csharp-compiler-as.htmlで提供されているサンプルを実行しようとしていますが、別のバージョンの mono を対象としているようです。たとえば、Compile は Evaluator の静的メソッドです。彼のサンプルに次の変更を加えましたが、機能していません。誰でも正しい変更を提供できますか?また、Mono.CSharp への API の変更に関する情報があるかどうかを知っている人はいますか? 私のコンパイラによって報告されたバージョンは次のとおりです。
$ dmcs --version
Mono C# compiler version 2.10.5.0
このコマンドラインを使用して次のコードをコンパイルしました。
dmcs -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);
}
}
}
}