Roslyn MsBuildWorkspace 编译发出不包含我的更改

Roslyn MsBuildWorkspace compilation emit does not contain my changes

我正在尝试使用 roslyn 对文档进行微小的更改,然后将项目编译为新的 dll。但是当我将它编译成一个新的 dll 时,我所有的更改都消失了。我在这里遗漏了什么吗?

var workspace = MSBuildWorkspace.Create();
var project = workspace.OpenProjectAsync(@"path\to\.csproj").Result;

var documents = project.DocumentIds;
foreach (var documentId in documents)
{
    var document = project.GetDocument(documentId);
    var root = document.GetSyntaxRootAsync().Result

    var rewrite = new MyRewrite();
    root = rewrite.Visit(root);

    var newDocument = document.WithSyntaxRoot(root);
    var compilation = newDocument.Project.GetCompilationAsync().Result;

    // When I look at the sementatic model here it contains my changes.
    var sementaticModel = 
    compilation.GetSemanticModel(newDocument.GetSyntaxTreeAsync().Result); 

    // But when I inspect this dll with dotPeek it's still the old code without changes.
    compilation.Emit("new/dll/path");   
}

不知何故它通过改变工作:

var newDocument = document.WithSyntaxRoot(root);

var newDocument = document.WithText(root.GetText());