5

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?

Amogh Natu
  • 781
  • 1
  • 10
  • 37

2 Answers2

10

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!

Community
  • 1
  • 1
Amogh Natu
  • 781
  • 1
  • 10
  • 37
8

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

PeterJ
  • 3,705
  • 28
  • 51
  • 71
Tushar
  • 790
  • 7
  • 12