问题
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