简体   繁体   中英

Roslyn get all solution project references

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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