gpt4 book ai didi

c# - StyleCop/FxCop 10 - 如何仅在命名空间级别正确抑制消息?

转载 作者:行者123 更新时间:2023-12-04 16:58:06 25 4
gpt4 key购买 nike

FxCop 10 提示以下问题:

using XYZ.Blah; //CA1709 - "XYZ"
using Xyz.Blah; //No complaint.

using XylophoneSuperDuperLongFullName.Blah; //I don't want to have a long full name for my company name.

问题是...我希望我的公司名称全部显示为大写,因为 XYZ 是缩写。名称的长版本太长而不能成为有用的命名空间。微软可以避免这种东西,因为它们的首字母缩写词只有 2 个字母。
using MS.Something; //No Complaint.
using Microsoft.SomethingElse; //No Complaint.

所以,我正在考虑添加一个 SuppressMessageAttribute抑制此警告。但是,我不确定如何正确地仅(或什至将其粘贴到何处)以使其仅影响此实例。我不想抑制该命名空间中的任何内容,因为我想捕捉我犯的任何其他错误。我确实查看了 msdn 和 google 搜索,但我找不到任何显示如何专门针对此实例的内容。我发现的最接近的是 Scope = "namespace"但我不确定这是否意味着它会影响实际的命名空间名称,或者它是否会影响该命名空间内的所有内容。

最佳答案

MSDN - CA1709: Identifiers should be cased correctly :

It is safe to suppress this warning if you have your own naming conventions, or if the identifier represents a proper name, for example, the name of a company or a technology.

You can also add specific terms, abbreviations, and acronyms that to a code analysis custom dictionary. Terms specified in the custom dictionary will not cause violations of this rule. For more information, see How to: Customize the Code Analysis Dictionary.



话虽如此,如果您觉得有理由压制该消息,那真的一点也不难。在 FxCop 10 中,右键单击要抑制的任何消息,然后转到复制为>抑制消息或复制为>模块级抑制消息。

您应该放置 SuppressMessageAttribute s 在适当的位置。抑制单个位置的属性应放置在该位置上,例如,方法、字段、属性或类的上方。

在您的实例中,没有放置属性的特定位置(默认情况下,它应该复制为 [module: SuppressMessage(...)] 。这是一个很好的迹象,如果它是模块级抑制,则它属于文件顶部特定于文件(例如,特定于文件的资源)。或者,更有可能的是,它属于 GlobalSuppressions.cs 文件。
using System.Diagnostics.CodeAnalysis;

[module: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Justification = "Because I said so!", MessageId = "XYZ", Scope = "namespace", Target = "XYZ.Blah")]

您也可以缩短 CheckId如果您愿意,可以使用属性,但最好知道 CA1709 的含义。如果你不喜欢,这也有效:
using System.Diagnostics.CodeAnalysis;

[module: SuppressMessage("Microsoft.Naming", "CA1709", Justification = "Because I said so!", MessageId = "XYZ", Scope = "namespace", Target = "XYZ.Blah")]

最后……除非您在构建中包含“CODE_ANALYSIS”符号,否则所有这些都将毫无结果。转到 Properties>Build 并添加条件编译符号。

关于c# - StyleCop/FxCop 10 - 如何仅在命名空间级别正确抑制消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14133699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com