我正在尝试使用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");
}
答案 0 :(得分:0)
不知何故,它改变了:
var newDocument = document.WithSyntaxRoot(root);
到
var newDocument = document.WithText(root.GetText());