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?
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!
You need to add a reference to Microsoft.CodeAnalysis.Workspaces.MSBuild.dll
which comes from the Nuget package Microsoft.CodeAnalysis.Workspaces.MSBuild
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With