Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Mono.Csharp中运行迷你程序_C#_Compiler Construction_Mono_Bootstrapping - Fatal编程技术网

C# 在Mono.Csharp中运行迷你程序

C# 在Mono.Csharp中运行迷你程序,c#,compiler-construction,mono,bootstrapping,C#,Compiler Construction,Mono,Bootstrapping,我正在尝试编写一个交互式C#教学应用程序,用户可以在其中试验/更改代码示例,看看会发生什么(有点像JSFIDLE) 我已经找到了很多小表达式或类似REPL的使用Mono.Csharp作为运行时编译器的例子,但是我找不到一个执行“迷你程序”的例子 这是我目前为止的玩具代码(MVC动作)。“code”参数直接从文本区域发布 [HttpPost] public ActionResult Index(string code) { var reportWriter = new StringWrit

我正在尝试编写一个交互式C#教学应用程序,用户可以在其中试验/更改代码示例,看看会发生什么(有点像JSFIDLE)

我已经找到了很多小表达式或类似REPL的使用Mono.Csharp作为运行时编译器的例子,但是我找不到一个执行“迷你程序”的例子

这是我目前为止的玩具代码(MVC动作)。“code”参数直接从文本区域发布

[HttpPost]
public ActionResult Index(string code)
{
    var reportWriter = new StringWriter();
    var settings = new CompilerSettings();
    var printer = new ConsoleReportPrinter(reportWriter);
    var reports = new Report(printer);
    var eval = new Evaluator(settings, reports);

    var model = new CodeViewModel();
    model.Code = code;
    eval.Run(code);
    model.Result = reportWriter.ToString();

    return View("Index", model);
}
现在假设代码是如下所示的字符串:

using System;
public class MyClass
{
    public void DoSomething()
    {
        Console.WriteLine("hello from DoSomething!");
    }
}
如何引导它(即实例化
MyClass
对象并在其上调用
DoSomething
)?我试着只添加
newmyclass().DoSomething()到最后,但我得到以下信息:

{interactive}(1,2): warning CS0105: The using directive for `System' appeared previously in this namespace
{interactive}(1,8): (Location of the symbol related to previous warning)
{interactive}(11,1): error CS1530: Keyword `new' is not allowed on namespace elements
{interactive}(11,4): error CS1525: Unexpected symbol `MyClass', expecting `class', `delegate', `enum', `interface', `partial', or `struct'

我遗漏了什么?

虽然这个问题已经被接受,但当我看到这篇文章时,我也遇到了类似的问题。我终于明白了,所以我想我应该在这里发帖,以防别人遇到麻烦

var reportWriter = new StringWriter();
var settings = new CompilerSettings();
var printer = new ConsoleReportPrinter(reportWriter);
var reports = new Report(printer);
var eval = new Evaluator(settings, reports);

eval.Run(code);

eval.Run(@"
    var output = new System.IO.StringWriter(); 
    Console.SetOut(output);
    new MyClass().DoSomething();");

var model = new CodeViewModel();
model.Code = code;

if (reports.Errors > 0)
   model.Result = reportWriter.ToString();
else
   model.Result = (string) eval.Evaluate("output.ToString();");

return View("Index", model);
在对
计算器的同一调用中,不能混合使用
和其他语句。在Mono.CSharp REPL上:

using声明必须单独出现,不能与常规语句在一行中混合


由于这是对每一行使用对
计算器的单独调用,因此同样的限制适用于从应用程序调用
计算器。

您可以发布包含MyClass类的整个文件吗?MyClass代码不在文件中,而是在字符串中。