【问题标题】:Using the Mono C# compiler as a service (error)将 Mono C# 编译器用作服务(错误)
【发布时间】:2011-01-01 16:09:10
【问题描述】:

我对 Mono 比较陌生,我正在尝试将 C# 脚本功能添加到我的应用程序中。我找到了 Miguel de Icaza 的 blog post。将 C# 编译器作为服务调用的方法是包含 Mono.CSharp 并使用 Evaluator 类,特别是 Evaluate 或 Run 方法。这记录在here。

这是我的示例代码(来自互联网上有关此主题的其他博客文章,是的,我已经完成了我的谷歌搜索)

using System;
using Mono.CSharp;

namespace EvalTest
{
  public class Test
  {
    static void Main(string [] args)
    {
      Mono.CSharp.Evaluator.Evaluate("using System;");
      Mono.CSharp.Evaluator.Run("using System;");
    }
  }
}

当我们尝试编译它时,我得到了这些错误:

eval.cs(10,19): error CS0234: The type or namespace name `Evaluator' does not exist in the namespace `Mono.CSharp'. Are you missing an assembly reference?
eval.cs(11,19): error CS0234: The type or namespace name `Evaluator' does not exist in the namespace `Mono.CSharp'. Are you missing an assembly reference?

在使用所有 Mono 编译器的 Linux 和 OSX 上也会发生同样的事情,我什至尝试了 silverlight 编译器。我在stackoverflow上搜索过类似的问题,每个人都参考了Miguel的博客文章和一些类似的示例代码。我错过了什么?我需要添加一些编译器标志吗?感谢您的帮助。

【问题讨论】:

    标签: c# mono


    【解决方案1】:

    您是否关注了这篇博文:

    使用很简单,必须引用`gmcs.exe'程序集

    ?这就是我对您的代码所做的(在 Test.cs 中):

    c:\Users\Jon\Test>copy "c:\Program Files (x86)\Mono-2.8"\lib\mono\2.0\gmcs.exe .
            1 file(s) copied.
    
    c:\Users\Jon\Test>gmcs /r:gmcs.exe Test.cs
    
    c:\Users\Jon\Test>mono Test.exe
    
    Unhandled Exception: System.ArgumentException:
      The expression did not set a result
      at Mono.CSharp.Evaluator.Evaluate (System.String input) [...]
      at EvalTest.Test.Main (System.String[] args) [...]
    

    这很公平,它没有给出任何结果——它只是一个 using 指令。不过这很好用:

    Mono.CSharp.Evaluator.Run("System.Console.WriteLine(5 + 5);");
    

    【讨论】:

      【解决方案2】:

      您是否缺少程序集参考?

      这是一种在 95% 的情况下往往是正确的心理错误信息。回到博文并注意这一行:

      使用很简单,必须引用`gmcs.exe'程序集

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多