使用 Roslyn 访问和修改解决方案中的所有文档

Visit and modify all documents in a solution using Roslyn

我想使用 Roslyn 遍历给定解决方案中每个项目的所有文档。

这是我现在的代码:

var msWorkspace = MSBuildWorkspace.Create();
var solution = await msWorkspace.OpenSolutionAsync(solutionPath);
foreach (var project in solution.Projects)
{
    foreach (var document in project.Documents)
    {
        if (document.SourceCodeKind != SourceCodeKind.Regular)
            continue;

        var doc = document;
        foreach (var rewriter in rewriters)
        {
            doc = await rewriter.Rewrite(doc);
        }

        if (doc != document)
        {
            Console.WriteLine("changed {0}",doc.Name);
            //save result

            //the solution is now changed and the next document to be processed will belong to the old solution
            msWorkspace.TryApplyChanges(doc.Project.Solution);
        }                    
    }
}

这里的问题是 Roslyn 在很大程度上是不可变的。 在第一个 "msWorkspace.TryApplyChanges" 之后,解决方案和文档现在已替换为新版本。

因此下一次迭代仍会遍历旧版本。 有没有办法以 Roslyn 惯用的方式做到这一点? 还是我必须求助于某种 for(int projectIndex = 0;projectIndex < solution.Projects.count) { 黑客技术?

Roslyn gitter 聊天中发布的这个解决方案可以解决问题。

var solution = await msWorkspace.OpenSolutionAsync(solutionPath);

foreach (var projectId in solution.ProjectIds)
{
    var project = solution.GetProject(projectId);
    foreach (var documentId in project.DocumentIds)
    {
        Document document = project.GetDocument(documentId);

        if (document.SourceCodeKind != SourceCodeKind.Regular)
            continue;

        var doc = document;
        foreach (var rewriter in rewriters)
        {
            doc = await rewriter.Rewrite(doc);

        }

        project = doc.Project;
    }
    solution = project.Solution;
}
msWorkspace.TryApplyChanges(solution);

在这种情况下,迭代之间的更改不再被丢弃,因为一切都建立在上一次迭代的结果之上。 (也就是说,文档和项目是通过 ID 获取的,而不是从遍历原始结构的枚举器获取的)