了StyleCop / FxCop的10 - 你如何适当地抑制仅在命名空间级别的消息? [英] StyleCop/FxCop 10 - How do you properly suppress a message only on a namespace level?

查看:126
本文介绍了了StyleCop / FxCop的10 - 你如何适当地抑制仅在命名空间级别的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

10的F​​xCop抱怨以下内容:

FxCop 10 is complaining about the following:

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个字母

The problem is... I want my company name to show up in all UPPERCASE because XYZ is an abbreviation. The long version of the name is much too long to be a useful namespace. Microsoft gets away with this kind of stuff because their acronym is only 2 letters.

using MS.Something; //No Complaint.
using Microsoft.SomethingElse; //No Complaint.



所以,我一直在寻找添加 SuppressMessageAttribute 要禁止这种警告。但是,我不知道如何正确地这样做只能(或者甚至把它贴),因此它只会影响这一个实例。我不希望该命名空间中禁止任何东西,因为我要赶任何其他错误,我做。我没有看在MSDN和谷歌搜索,但我找不到任何说明如何具体目标只是这个实例。我发现的最接近余地=命名空间,但我不知道,如果这意味着它会影响实际的命名空间名称,或者如果它影响一切内的命名空间。

So, I was looking at adding a SuppressMessageAttribute to suppress this warning. But, I'm not sure how to do so properly to only (or where to even stick it) so that it ONLY affects this one instance. I don't want to Suppress anything within that namespace because I want to catch any other mistakes I make. I did look at at the msdn and google searched but I can't find anything that shows how to specifically just target this instance. The closest I found was Scope = "namespace" but I wasn't sure if that means it affects the actual namespace name or if it affects everything WITHIN that namespace.

推荐答案

MSDN - CA1709:标识符应正确套管:在

这是安全的,如果$ b $或者当标识符表示A b你有自己的命名惯例,
,来禁止这种警告
适当的名称,例如,
A公司或技术的名称。

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.

您还可以添加特定的术语,
缩写,和缩写,为一个
代码分析自定义词典。术语美元的自定义词典
规定不会导致违反本
规则B $湾有关详细信息,请参阅如何
到:自定义代码分析
字典

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右键单击要抑制和去复制为>禁止留言或者复制为>模块级抑制消息的任何消息。


That being said, if you feel justified to suppress the message, it really isn't hard at all. In FxCop 10 right click on any message you want to suppress and go to Copy As>Suppress-Message or Copy As>Module-level Suppress Message.

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

You should place the SuppressMessageAttributes in the appropriate locations. Attributes that suppress a single location should be placed on that location, for example, above a method, field, property, or class.

在你例如,有没有放置该属性(默认情况下具体位置应该拷贝过来的 [模块:SuppressMessage(...)。这是一个很好的迹象,它属于无论是在一个文件的顶部,如果它是一个模块级抑制特定到一个文件(例如,特定于一个文件中的资源),或者,更可能的是,它是属于在一个GlobalSuppressions.cs文件

In you're instance, there is no specific location to place the attribute (by default it should copy over as [module: SuppressMessage(...)]. This is a good indication that it belongs either at the top of a file if it is a module-level suppression particular to a file (for example, to a resource specific to a file). Or, and more likely, it belongs in a GlobalSuppressions.cs file.

using System.Diagnostics.CodeAnalysis;

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

您还可以缩短 CheckId 属性,如果你想,但它的好,知道什么CA1709意思。如果你不觉得喜欢它,这也是作品:

You can also shorten the CheckId property if you want to, but it's good to know what CA1709 means. If you don't feel like it, this also works:

using System.Diagnostics.CodeAnalysis;

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

和最后......这一切都将是徒劳的,除非你包括CODE_ANALYSIS符号构建。转到属性>生成并添加条件编译符号。

And lastly... all this will be fruitless unless you include the 'CODE_ANALYSIS' symbol in your build. Go to Properties>Build and add the conditional compilation symbol.

这篇关于了StyleCop / FxCop的10 - 你如何适当地抑制仅在命名空间级别的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆