为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
智慧大石 2023-09-09 17:06:41
我们在项目中使用 StyleCop 分析器。我的任务之一是停用一些规则,但不创建GlobalSuppressions.cs文件。我找到了解决方案,但仅限于创建此文件,所以我很困惑。
查看完整描述

1 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

要抑制 之外的警告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;

如果您想全局禁用它们,可以在解决方案的根目录下使用.editorconfig文件。


root = true

[*.{cs,vb}]

dotnet_diagnostic.IDE0052.severity = none

您还可以配置规则集文件,但现已弃用。


在您的 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>


查看完整回答
反对 回复 2023-09-09
  • 1 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信