How to use Roslyn Code Analysis API with MVC 6 pro

2019-08-08 05:58发布

I intend to use the new Roslyn Code Analysis API with an MVC 6 project.

However, when I run this code:

string pathToSolution = @"..\..\..\WebApplicationComplex.sln";
const string projectName = "RoslynWebAPIProject";
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
Solution solutionToAnalyze = workspace.OpenSolutionAsync(pathToSolution).Result;
Project sampleProjectToAnalyze = solutionToAnalyze.Projects.Where((proj) => proj.Name == projectName).FirstOrDefault();
Compilation sampleToAnalyzeCompilation = sampleProjectToAnalyze.GetCompilationAsync().Result;

I only get the analyzer project and nothing of the MVC 6 project, so the sampleProjectToAnalyze remains null.

How should I analyze that type of projects?

1条回答
乱世女痞
2楼-- · 2019-08-08 06:04

You need to use the DNX Workspace.
Add a reference to that package, then create a new ProjectJsonWorkspace(jsonPath)

查看更多
登录 后发表回答