Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

roslyn

msbuild

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).

enter image description here

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?

like image 258
Amogh Natu Avatar asked Dec 05 '22 02:12

Amogh Natu


2 Answers

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)

enter image description here

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!

like image 57
Amogh Natu Avatar answered May 13 '23 21:05

Amogh Natu


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

like image 21
Tushar Avatar answered May 13 '23 20:05

Tushar