简体   繁体   English

Mono.CSharp(编译器即服务)在版本2.10中更改

[英]Mono.CSharp (Compiler as a Service) changes in version 2.10

I am running Mono version 2.10 on Ubuntu 11.10. 我在Ubuntu 11.10上运行Mono版本2.10。 I am trying to run the sample provided on http://blog.davidebbo.com/2012/02/quick-fun-with-monos-csharp-compiler-as.html ,but it seems to target a different version of mono. 我正在尝试运行http://blog.davidebbo.com/2012/02/quick-fun-with-monos-csharp-compiler-as.html上提供的示例,但它似乎针对不同版本的mono。 For instance Compile is a static method on Evaluator. 例如,Compile是Evaluator上的静态方法。 I have made the following changes to his sample, but haven't got it working. 我对他的样本进行了以下更改,但是没有使用它。 Can anyone provide the correct changes, and does anyone know if there is any info on the API changes to Mono.CSharp? 任何人都可以提供正确的更改,并且有人知道有关Mono.CSharp的API更改的任何信息吗? The version reported by my compiler is as follows: 我的编译器报告的版本如下:

$ dmcs --version
Mono C# compiler version 2.10.5.0

I compiled the following code using this command line: 我使用此命令行编译了以下代码:

dmcs -r:Mono.CSharp Sample.cs dmcs -r:Mono.CSharp Sample.cs

And got this warning when compiling. 编译时收到此警告。

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)

This is the result of running the code: 这是运行代码的结果:

$ ./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)

This is the Code I have so far: 这是我到目前为止的守则:

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 comes with Evaluator supporting expressions and statements only. Mono 2.10附带了Evaluator支持表达式和语句。 Your code contains type declaration which is not supported by Mono 2.10. 您的代码包含Mono 2.10不支持的类型声明。

Mono 2.11 or git master Mono.CSharp have support for type declarations and other advanced features. Mono 2.11或git master Mono.CSharp支持类型声明和其他高级功能。

According to this: http://www.mono-project.com/Release_Notes_Mono_2.12#Instance_API , the static/global Evaluator is the older API and the Instance API is the newer one. 根据这个: http//www.mono-project.com/Release_Notes_Mono_2.12#Instance_API ,静态/全局Evaluator是较旧的API,而Instance API是较新的。 The Mono I have is the current stable version (2.10) and Mono.CSharp that comes with version 2.11 has the instance methods. 我拥有的Mono是当前的稳定版本(2.10),版本2.11附带的Mono.CSharp具有实例方法。 2.12 doesn't look to have been released yet. 2.12看起来还没有发布。

Here's another mention of the instance API to the Compiler as a service: http://tirania.org/blog/archive/2011/Oct-14.html 以下是编译器作为服务的实例API的另一个提及: http//tirania.org/blog/archive/2011/Oct-14.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM