Type or namespace 'MSBuild' does not exist in namespace 'Microsoft.CodeAnalysis' despite being able to navigate to definition

佐手、 提交于 2020-01-04 14:20:33

问题


I'm trying out Roslyn for the first time and I'm writing a small piece of code to read through a project, classes and class members.

I'm using the MSBuildWorkspace class to create the Roslyn workspace (MSBuildWorkspace.Create()). Below is a small part of the code I've written

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;    //Some problem in this line? Read on.

...
...

var workspace = MSBuildWorkspace.Create();
Solution solutionToAnalyze =
                workspace.OpenSolutionAsync(pathToSolution).Result;
IEnumerable<Project> projectsToAnalyze =
                solutionToAnalyze.Projects;
...
...

When I do a "Go to Definition" on MSBuildWorkspace class, I'm able to navigate to the class's definition and I can clearly see it's namespace to be Microsoft.CodeAnalysis.MSBuild (See image below).

But despite this, I keep getting the error message, "The type or namespace MSBuild doesn't exist in the namespace 'Microsoft.CodeAnalysis' at the using statement that I've highlighted with the comment". I just can't seem to get the reason behind this error. Am I missing anything?


回答1:


I found the answer thanks to the link provided by @CZabransky.

https://stackoverflow.com/a/23621818/2377928

Basically I was overlooking the below warning that I was getting. (One more reason why one SHOULDN'T overlook warnings! o_O)

My project was targeting Framework version 4.5 and so this assembly was not building as it was built against the v4.5.2 version. I had to target the framework version to 4.6 and the solution built successfully.

Hope this helps!




回答2:


You need to add a reference to Microsoft.CodeAnalysis.Workspaces.MSBuild.dll which comes from the Nuget package Microsoft.CodeAnalysis.Workspaces.MSBuild.



来源:https://stackoverflow.com/questions/31706424/type-or-namespace-msbuild-does-not-exist-in-namespace-microsoft-codeanalysis

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