![](/img/trans.png)
[英]C# Compiler-as-a-Service: Mono.CSharp vs Microsoft.CSharp
[英]Mono.CSharp (Compiler as a Service) changes in version 2.10
我在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);
}
}
}
}
Mono 2.10附帶了Evaluator支持表達式和語句。 您的代碼包含Mono 2.10不支持的類型聲明。
Mono 2.11或git master Mono.CSharp支持類型聲明和其他高級功能。
根據這個: http : //www.mono-project.com/Release_Notes_Mono_2.12#Instance_API ,靜態/全局Evaluator是較舊的API,而Instance API是較新的。 我擁有的Mono是當前的穩定版本(2.10),版本2.11附帶的Mono.CSharp具有實例方法。 2.12看起來還沒有發布。
以下是編譯器作為服務的實例API的另一個提及: http : //tirania.org/blog/archive/2011/Oct-14.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.