Question

I am trying to open the solution and want to refer the project.. but solution.Projects always comes empty. Below is my code. Am I missing something?

var workspace = MSBuildWorkspace.Create(ImmutableDictionary<string, string>.Empty);
var solution = workspace.OpenSolutionAsync(solutionPath).Result;
var project = solution.Projects.FirstOrDefault(p => p.Name == projectName); //Here solution.Projects comes empty(count 0)
Était-ce utile?

La solution

If I'm reading your comments right, your code is running within Visual Studio, and you're trying to get a workspace to match whatever solution is currently open. In that case, do not use MSBuildWorkspace. MSBuildWorkspace really should never be used inside devenv.exe. Follow the answers to this question which lets you get direct access to the Roslyn model that we've already built up atop the solution model.

Autres conseils

Make sure that Microsoft.CodeAnalysis.CSharp.Workspaces package is installed. It helped me.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top