Does Roslyn actually allow you to manipulate TreatWarningsAsErrors for a CSharp project?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 02:54:08

问题


I'm trying to retrieve the setting TreatWarningsAsErrors, but I'm unable to find it for a project of my loaded solution.

What I'm trying to accomplish, is to get the setting from the project files, and set it to true, if it's not already that. Next, I want to let Roslyn do a compilation with the new setting, so I can check if this will break the project.

I've looked at various places, among others, the Project.CompilationOptions. Most options to a project build are there, except this one.

The CompilationOptions contains all the build settings, such as warning level, etc. But TreatWarningsAsErrors is not there. Am I looking at the wrong place?

The way I'm opening the solution is similar to the FormatSolution sample:

var solutionFile = @"C:\ties\src\playground\EnforceTreatAllWarningsAsErrors\EnforceTreatAllWarningsAsErrors.sln";

var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionFile).Result;
var project = solution.Projects.Single();

// warning level is there
var warningLevel = project.CompilationOptions.WarningLevel;

// treat warnings as errors is not there... The following doesn't compile :(
bool treatWarningsAsErrors = project.CompilationOptions.TreatWarningsAsErrors;

回答1:


You're looking for

compilationOptions.WithGeneralDiagnosticOption(ReportDiagnostic.Error)

Source



来源:https://stackoverflow.com/questions/30458213/does-roslyn-actually-allow-you-to-manipulate-treatwarningsaserrors-for-a-csharp

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