编译器即服务执行上下文
本文关键字:上下文 执行 服务 编译器 | 更新日期: 2023-09-27 18:27:15
我能做些什么来使这段代码正常工作吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.CSharp;
namespace MonoREPLTester
{
public class Program
{
static void Main(string[] args)
{
var evaluator = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()));
string mystring="hello";
object result = evaluator.Run("mystring.IndexOf('e');");
Console.WriteLine("Result: "+result);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
我目前收到一个警告,因为Mono上下文不知道mystring
。
"错误CS0103:名称'mystring'在当前上下文中不存在"
您需要使其成为一个静态的类级变量:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.CSharp;
using System.Reflection;
namespace MonoREPLTester
{
public class Program
{
public static string mystring = "hello";
static void Main(string[] args)
{
var evaluator = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()));
evaluator.ReferenceAssembly(Assembly.GetExecutingAssembly());
evaluator.Run("using MonoREPLTester;");
object result = evaluator.Evaluate("Program.mystring.IndexOf('l');");
Console.WriteLine("Result: " + result);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}