Create workspace from MSBuild project

suggest change

Add the Microsoft.CodeAnalysis.CSharp.Workspaces NuGet package before continuing.

var workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(projectFilePath);
var compilation = await project.GetCompilationAsync();

foreach (var diagnostic in compilation.GetDiagnostics()
    .Where(d => d.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error))
{
    Console.WriteLine(diagnostic);
}

This loads an existing project, compiles it, and reports errors. The result stays in memory, where both the syntax and the semantic model are available.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents