简体   繁体   English

使用Mono C#编译器作为服务(错误)

[英]Using the Mono C# compiler as a service (error)

I'm relatively new to Mono and I am trying to add C# scripting capabilities to my application. 我对Mono比较陌生,我正在尝试为我的应用程序添加C#脚本功能。 I found this blog post by Miguel de Icaza. 我在Miguel de Icaza发现了这篇博文 The way to call the C# compiler as a service is to include Mono.CSharp and use the Evaluator class, specifically the Evaluate or Run methods. 将C#编译器作为服务调用的方法是包含Mono.CSharp并使用Evaluator类,特别是Evaluate或Run方法。 This is documented here . 在此处记录

So here is my example code (derived from the other blog posts on the internet on this subject, yes I've done my googling) 所以这是我的示例代码(来自互联网上关于这个主题的其他博客文章,是的,我已经完成了我的谷歌搜索)

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;");
    }
  }
}

And when we try to compile it I get these errors: 当我们尝试编译它时,我得到这些错误:

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?

The same thing happens on Linux and OSX using all the Mono compilers, I even tried the silverlight one. 在使用所有Mono编译器的Linux和OSX上也发生了同样的事情,我甚至试过了Silverlight。 I have searched stackoverflow for similar questions, everyone references Miguel's blog post and some similar sample code. 我搜索了stackoverflow的类似问题,每个人都参考了Miguel的博客文章和一些类似的示例代码。 What am I missing? 我错过了什么? Is there some compiler flag I need to add? 我需要添加一些编译器标志吗? Thanks for your help. 谢谢你的帮助。

Did you follow this bit of the blog post: 你有没有关注这篇博文:

Usage is very simple, you must reference the `gmcs.exe' assembly 用法很简单,你必须参考`gmcs.exe'程序集

? This is what I did with your code (in Test.cs): 这就是我对你的代码所做的(在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) [...]

It's fair enough that it doesn't give any result - it's only a using directive. 这是公平的,它不会给出任何结果 - 它只是一个使用指令。 This works fine though: 这工作正常:

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

Are you missing an assembly reference? 你错过了装配参考吗?

It's one of those psychic error messages that tends to be right 95% of the time. 这是95%的时候往往是正确的心理错误信息之一。 Go back to the blog post and note this line: 回到博客文章并注意这一行:

Usage is very simple, you must reference the `gmcs.exe' assembly 用法很简单,你必须参考`gmcs.exe'程序集

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

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