using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
class Script
{
ScriptState state;
public static async Task<Script> Create(
IEnumerable<Assembly> references, IEnumerable<string> usings)
{
var options = ScriptOptions.Default.WithReferences(references).WithImports(usings);
var state = await CSharpScript.RunAsync("", options);
return new Script() { state = state };
}
public async Task<object> ExecuteNext(string code)
{
state = await state.ContinueWithAsync(code);
return state.ReturnValue;
}
}
这实际上是整个复杂性。
像这样使用:
// какие DLL должны быть доступны?
static Assembly[] references = new[]
{
typeof(object).Assembly,
typeof(Uri).Assembly,
typeof(Enumerable).Assembly,
typeof(MessageBox).Assembly
};
// какие юзинги должны быть доступны?
static string[] usings = new[]
{
"System",
"System.IO",
"System.Text",
"System.Windows"
};
让我们使用新奇的 Roslyn API。
首先,我们需要一个 nuget 包
Microsoft.CodeAnalysis.CSharp.Scripting,通过References→ 右键单击 →添加它Manage NuGet Packages...。现在,脚本本身。模型类将非常简单,因为一切都已经为我们编写好了:
这实际上是整个复杂性。
像这样使用:
我搞砸了最简单的界面,它看起来像这样:
该项目在这里:https ://github.com/vladd/EvalUITest
各种有用的文献:
原始文章在这里。