简体   繁体   English

MSBuildWorkspace:解决方案在Visual Studio 2019上不包含任何文档和项目

[英]MSBuildWorkspace : solution contains no documents and no projects on Visual Studio 2019

I want to create an analyzer using roslyn, but first i need to get all the documents(.cs files) from the target solution. 我想使用roslyn创建一个分析器,但首先我需要从目标解决方案中获取所有文档(.cs文件)。

i used the following code from Josh Varty's tutorial 我使用了Josh Varty教程中的以下代码

        string solutionPath = @"C:\Users\hamza\Desktop\TestSolution\TestSolution.sln";
        var msWorkspace = MSBuildWorkspace.Create();

        var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;
        foreach (var project in solution.Projects)
        {
            Console.WriteLine(project);
            foreach (var document in project.Documents)
            {
                Console.WriteLine(project.Name + "\t\t\t" + document.Name);
            }
        }

But the result is null, i don't get any documents or projects. 但结果是null,我没有得到任何文件或项目。

the MSBuildWorkspace version is 3.0.0 i tried also 2.10.0 but the result is the same. MSBuildWorkspace版本是3.0.0我也试过2.10.0但结果是一样的。

anyone have an idea about this ? 有人对此有所了解吗? or how to fix this ? 或者如何解决这个问题?

After more research I found this helpful issue post on Github: https://github.com/dotnet/roslyn/issues/24767 经过更多研究后,我在Github上发现了这个有用的问题: https//github.com/dotnet/roslyn/issues/24767

this code worked fine 这段代码工作正常

 var projectPath = @"C:\Users\hamza\Desktop\TestSolution\TestSolution.sln";
        using (var workspace = MSBuildWorkspace.Create())
        {
            var solution = workspace.OpenSolutionAsync(projectPath).Result;

            foreach (var project in solution.Projects)
            {
                foreach (var document in project.Documents)
                {
                    Console.WriteLine(project.Name + "\t\t\t" + document.Name);
                }
            }
        }

Finally to make things works fine i added this package: 最后,为了使事情正常,我添加了这个包:

Install-Package Buildalyzer.Workspaces -Version 2.2.0 Install-Package Buildalyzer.Workspaces -Version 2.2.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM