将多个更改应用于roslyn中的解决方案 [英] Applying multiple changes to a solution in roslyn

查看:60
本文介绍了将多个更改应用于roslyn中的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将更改应用于解决方案的多个文档,但仅反映出第一个更改,其余的则被拒绝。链接显示了仅一次将更改应用于解决方案的情况。解决该问题的方法是什么。我将不胜感激指向解决方案或代码段的链接。

I want to apply changes to several documents of a solution but only the first change is reflected and rest of them are rejected.This link shows how only once can changes be applied to a solution. What would be a work around for this. I would appreciate a link directing to a solution or a code snippet.

这是我的功能:

public static async Task<bool> AddMethod(string solutionPath)
{
    var workspace = MSBuildWorkspace.Create(); 
    var solution = await workspace.OpenSolutionAsync(solutionPath);
    ClassDeclarationSyntax cls = SyntaxFactory.ClassDeclaration("someclass");

    foreach (var project in solution.Projects)
    {
        foreach(var document in project.Documents)
        {
            Document doc = project.GetDocument(document.Id);
            var root = await doc.GetSyntaxRootAsync();
            var classes = root.DescendantNodes().Where(n => n.IsKind(SyntaxKind.ClassDeclaration));
            if (classes.Count() != 0)
            {
                SyntaxNode FirstClass = classes.First() as ClassDeclarationSyntax;
                if (FirstClass != null)
                {
                    var newRoot = root.ReplaceNode(FirstClass, cls);
                    doc = doc.WithText(newRoot.GetText());
                    Project proj = doc.Project;
                    var abc = workspace.TryApplyChanges(proj.Solution);
                }
            }
        }
    }
    return true;
}


推荐答案

解决方法是使用 ProjectId DocumentId 并一次将所有更改应用于工作区。

The workaround is to use ProjectId and DocumentId and apply all your changes to the workspace at one time.

尝试以下操作:

public static async Task<bool> AddMethod(string solutionPath)
{
    var workspace = MSBuildWorkspace.Create();
    var solution = await workspace.OpenSolutionAsync(solutionPath);
    ClassDeclarationSyntax cls = SyntaxFactory.ClassDeclaration("someclass");

    foreach (var projectId in solution.ProjectIds)
    {
        var project = solution.GetProject(projectId);
        foreach (var documentId in project.DocumentIds)
        {
            Document doc = project.GetDocument(documentId);
            var root = await doc.GetSyntaxRootAsync();
            var firstClass = root.DescendantNodes().OfType<ClassDeclarationSyntax>().FirstOrDefault();
            if (firstClass == null)
                return true;

            var newRoot = root.ReplaceNode(firstClass, cls);
            doc = doc.WithText(newRoot.GetText());
            //Persist your changes to the current project
            project = doc.Project;
        }

        //Persist the project changes to the current solution
        solution = project.Solution;
    }
    //Finally, apply all your changes to the workspace at once.
    var abc = workspace.TryApplyChanges(solution);
    return true;
}

这篇关于将多个更改应用于roslyn中的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆