私はインタラクティブなC#教育アプリを作成しようとしています。このアプリでは、ユーザーがコード例を実験/変更して、何が起こるかを確認できます(jsfiddleのようなもの)。
小さな式やREPLのようなMono.Csharpのランタイムコンパイラとしての使用例をたくさん見つけましたが、「ミニプログラム」が実行される例はまったく見つかりません。
これが私のこれまでのおもちゃのコードです(MVCアクション)。「コード」パラメータは、テキストエリアから直接投稿されます。
[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
)?new MyClass().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'
私は何が欠けていますか?