Roslyn实际上是否允许您为CSharp项目操作TreatWarningsAsErrors?

时间:2015-05-26 12:11:27

标签: c# msbuild compiler-warnings roslyn

我正在尝试检索TreatWarningsAsErrors设置,但我无法找到我已加载解决方案的项目。

我想要完成的是从项目文件中获取设置,并将其设置为true,如果不是那样的话。接下来,我想让Roslyn使用新设置进行编译,这样我就可以检查这是否会破坏项目。

我看过Project.CompilationOptions等各个地方。项目构建的大多数选项都在那里,除了这个。

CompilationOptions包含所有构建设置,例如警告级别等。但TreatWarningsAsErrors不存在。我在找错了地方吗?

我打开解决方案的方式类似于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 个答案:

答案 0 :(得分:1)

您正在寻找

compilationOptions.WithGeneralDiagnosticOption(ReportDiagnostic.Error)

Source