【问题标题】:Mono.CSharp (Compiler as a Service) changes in version 2.10Mono.CSharp(编译器即服务)在 2.10 版中的更改
【发布时间】:2012-04-25 09:43:46
【问题描述】:

我在 Ubuntu 11.10 上运行 Mono 2.10 版。我正在尝试运行http://blog.davidebbo.com/2012/02/quick-fun-with-monos-csharp-compiler-as.html 上提供的示例,但它似乎针对的是不同版本的单声道。例如 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);
            }
        }
    }
}

【问题讨论】:

  • 根据this,旧的 Evaluator 是静态的,新的 Evaluator 是成员。我必须检查我的 dll 版本。

标签: c# mono compiler-as-a-service dmcs


【解决方案1】:

Mono 2.10 带有仅支持表达式和语句的 Evaluator。您的代码包含 Mono 2.10 不支持的类型声明。

Mono 2.11 或 git master Mono.CSharp 支持类型声明和其他高级功能。

【讨论】:

    【解决方案2】:

    据此:http://www.mono-project.com/Release_Notes_Mono_2.12#Instance_API,静态/全局评估器是较旧的 API,而实例 API 是较新的。我拥有的 Mono 是当前的稳定版本(2.10),2.11 版本附带的 Mono.CSharp 具有实例方法。 2.12 好像还没有发布。

    这里还提到了 Compiler as a service 的实例 API:http://tirania.org/blog/archive/2011/Oct-14.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 2022-12-21
      • 2020-12-22
      相关资源
      最近更新 更多