Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn get all solution project references

Tags:

c#

roslyn

I am using Roslyn to read the referenced projects of a solution like this:

MSBuildWorkspace workspace = MSBuildWorkspace.Create();
workspace.LoadMetadataForReferencedProjects = true;
Solution solution = workspace.OpenSolutionAsync(Solution).Result;
foreach (var p in solution.Projects) {
   //do something with the project
}

The problem is that the solution variable does not contain projects where the files are not available locally. Meaning the project could not be loaded because the files are missing.

Is there a way to get all project references including the references where the project files are missing?

like image 430
doorman Avatar asked Mar 07 '23 10:03

doorman


1 Answers

There is a way to load all projects configured int the sln file. However, it is not through roslyn.

Microsoft Build exposes Microsoft.Build.Construction.SolutionFile.

You can get projects serveral ways, such as:

SolutionFile solutionInfo = SolutionFile.Parse(@"/path-to-sln.sln");
IEnumerable<ProjectInSolution> projectList = solutionInfo.ProjectsInOrder;

From here you could use workspace.OpenProjectAsync(project.AbsolutePath) to load available projects. (Note, workspace is a MSBuildWorkspace and project is a ProjectInSolution).

This is actually what roslyn is doing under the hood through MSBuildProjectLoader. However, it only supports two behaviors for handling missing projects: log or throw exception. If you would rather throw an exception for missing projects, set workspace.SkipUnrecognizedProjects = false;

like image 147
farlee2121 Avatar answered Mar 10 '23 12:03

farlee2121