是否可以选择在不创建“ GlobalSuppressions.cs”文件的情况下停用样式警察规则?

时间:2019-08-19 16:50:50

标签: c# stylecop

我们在项目中使用StyleCop Analyzer。我的任务之一是停用一些规则,但不创建GlobalSuppressions.cs文件。

我找到了解决方案,但只能创建此文件,所以我很困惑。

1 个答案:

答案 0 :(得分:1)

要禁止显示GlobalSuppressions.cs以外的警告,您可以:

使用注释(请记住将警告还原到范围之外!)

#pragma warning disable IDE0052 // Remove unread private members
private readonly Object _obj;
#pragma warning restore IDE0052 // Remove unread private members

或就地使用SuppressMessage属性

[SuppressMessage("Code Quality", "IDE0052:Remove unread private members", Justification = "<Pending>")]
private readonly Object _obj;

如果要全局禁用它们,还可以配置规则集文件。

在您的csproj中:

<PropertyGroup>
    <CodeAnalysisRuleSet>File.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

,然后根据需要创建File.ruleset。看起来很像

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Microsoft Managed Recommended Rules" Description="These rules focus on the most critical problems in your code, including potential security holes, application crashes, and other important logic and design errors. It is recommended to include this rule set in any custom rule set you create for your projects." ToolsVersion="10.0">
  <Rules AnalyzerId="Microsoft.CodeQuality.Analyzers" RuleNamespace="Microsoft.CodeQuality.Analyzers">
    <Rule Id="CA1056" Action="None" />
  </Rules>
  </Rules>
</RuleSet>

更多详情,请访问https://docs.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019#rule-sets