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

时间:2018-01-21 11:38:17

标签: c# roslyn

我正在尝试使用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");   
}

1 个答案:

答案 0 :(得分:0)

不知何故,它改变了:

var newDocument = document.WithSyntaxRoot(root);

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