允许您为CSharp项目操纵TreatWarningsAsErr

允许您为CSharp项目操纵TreatWarningsAsErr

本文介绍了Roslyn是否实际上允许您为CSharp项目操纵TreatWarningsAsErrors?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我要完成的工作是从项目文件中获取设置,并将其设置为true(如果尚未设置的话).接下来,我想让Roslyn使用新设置进行编译,以便检查是否会破坏项目.

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.

我看过很多地方,其中包括Project.CompilationOptions.除了该选项外,项目构建的大多数选项都在其中.

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

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

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

我打开解决方案的方式类似于 FormatSolution sample :

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;

推荐答案

您正在寻找

compilationOptions.WithGeneralDiagnosticOption(ReportDiagnostic.Error)

这篇关于Roslyn是否实际上允许您为CSharp项目操纵TreatWarningsAsErrors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:19