是否可以修改 Roslyn 中的 SyntaxTree 和 运行 编辑后的代码?

Is it possible to modify a SyntaxTree in Roslyn and run the edited code?

我正在使用 Roslyn 更改代码,但是,在更改后生成了新的 SyntaxNode,但我无法找到执行此新代码的方法。我发现的唯一一个是从新的 Root 中获取 ToString 并使用新字符串调用 EvaluateAsync 。应该有一种方法可以胜过它,因为我已经编译了新代码。

static void Main(string[] args)
{
    var expression = "System.Console.WriteLine(\"Test\")";
    var compile = CSharpScript.Create<EntityRepresentation>(expression).GetCompilation();
    var root = compile.SyntaxTrees.Single().GetRoot();

    var descentands = root.DescendantNodes().Where(n =>
    {
        if (n is ArgumentSyntax)
            return true;
        return false;
    }).ToList();

    var otherRoot = root.ReplaceNodes(descentands, (n1, n2) =>
    {
        var argumentName = Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal("NewValue")));
        return argumentName;
    });

    var newCode = otherRoot.ToString();

    // Faz o que estou querendo, contudo não me parece a melhor maneira
    var result = CSharpScript.EvaluateAsync(newCode).Result;
}

不幸的是,从语法树创建脚本对象的方法是 Microsoft 程序集的内部方法。

但是,您不必编译两次 - 您可以只解析第一次,然后再编译第二次。

var expression = "System.Console.WriteLine(\"Test\")";
var origTree = CSharpSyntaxTree.ParseText(expression, 
                  CSharpParseOptions.Default.WithKind(SourceCodeKind.Script));
var root = origTree.GetRoot();

// -Snip- tree manipulation

var script = CSharpScript.Create(otherRoot.ToString());
var errors = script.Compile();
if(errors.Any(x => x.Severity == DiagnosticSeverity.Error)) {
    throw new Exception($"Compilation errors:\n{string.Join("\n", errors.Select(x => x.GetMessage()))}");
}
await script.RunAsync();