How to load solution in Roslyn?

人盡茶涼 提交于 2020-05-29 07:47:49

问题


Here is my code that used to work:

MSBuildWorkspace msBuild = MSBuildWorkspace.Create();
Solution sln = await msBuild.OpenSolutionAsync(solutionPath);

but now I get:

int pr = sln.Projects.Count();//is 1 instead of 2.

and:

int docs = sln.Projects.First().Documents.Count();//is 0

Have they changed it again?

(If it matters, I need the Solution in order to iterate over fields etc. and use Renamer.RenameSymbolAsync .)


回答1:


The way I did it was like so:

if (!MSBuildLocator.IsRegistered) MSBuildLocator.RegisterDefaults();
using (var w = MSBuildWorkspace.Create())
    {
        var sln = await w.OpenSolutionAsync(solutionPath);
    }

I believe the MSBuildLocator.RegisterDefaults() is the missing piece here. it ensures the correct msbuild path for your VS version. It might be that your environment path was correct, but then you installed another instance of VS and it's got confused somehow



来源:https://stackoverflow.com/questions/61048374/how-to-load-solution-in-roslyn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!