StyleCop 和 FxCop 规则相互排除
我正在使用 StyleCop 和 FxCop 工具来改进我的代码,但我发现有两条规则,一条在 StyleCop 中,一条在 FxCop 中,它们相互排斥。如果我修复代码以匹配 StyleCop 的规则,则 FxCop 验证将失败,反之亦然。
第一条规则是来自 StyleCop 的 SA1200,它规定所有 using 指令必须放置在命名空间内。
所有 using 指令必须放置在命名空间内。
所以我做了这样的事情,
namespace MyNamespace
{
using System;
...
}
对于 StyleCop 来说没问题,不再有警告。现在我运行 FxCop 验证,它告诉我违反了 CA1014:
使用 CLSCompliant(true) 标记“MyApp.dll”,因为它公开了外部可见的类型。
为了解决这个问题,我应该这样做:
[ClsCompliant(true)]
namespace MyNamespace
{
...
}
但现在我无法构建我的项目,因为 ClsCompliant
属性无法识别(因为它来自 System
命名空间,我将其包含在 <代码>我的命名空间)。因此,如果我将 using System;
指令移到 MyNamespace
声明之外。这将使我的代码编译,但再次违反 StyleCop 的规则。
除了禁用 StyleCop 或 FxCop 中的其中一个规则之外,还有其他方法可以解决此问题吗?如果不可能,我应该禁用哪些规则?哪个不太重要?
I'm using StyleCop and FxCop tools to improve my code but I came to a place where there are two rules, one in StyleCop and one in FxCop that exclude each other. If I fix my code to match the rule from StyleCop then FxCop validation fails and vice versa.
First rule is SA1200 from StyleCop which says that all using directives must be placed inside of the namespace.
All using directives must be placed inside of the namespace.
So I have done something like this
namespace MyNamespace
{
using System;
...
}
It was ok for StyleCop, no more warnings. Now I run FxCop validation and it tells me that CA1014 is violated:
Mark 'MyApp.dll' with CLSCompliant(true) because it exposes externally visible types.
To resolve this I should do something like this:
[ClsCompliant(true)]
namespace MyNamespace
{
...
}
but now I cannot build my project because ClsCompliant
attribute is not recognized (because it's from System
namespace which I include inside of the MyNamespace
). So if I move using System;
directive outside of MyNamespace
declaration. This will make my code compile but again it will break the rule from StyleCop.
Is there any way to deal with this problem except for disabling one of the rules in StyleCop or FxCop? And if that's not possible which of the rules should I disable? Which is less important?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用完整的属性名称:
顺便说一句:如果要将整个程序集标记为 CLSCompliant,请放入
Properties/AssemblyInfo.cs 文件
Use full attribute name:
BTW: if you want to mark your whole assembly as CLSCompliant, put
in Properties/AssemblyInfo.cs file
我的建议是关闭“所有 using 指令必须放置在命名空间内”。 StyleCop 中的规则。坚持它是不切实际的,特别是因为大多数代码生成器(甚至 VS 自己的代码生成器)都不遵循这种做法。
My suggestion is to turn off the "All using directives must be placed inside of the namespace." rule in StyleCop. It's impractical to adhere to it, especially since most of the code generators (even VS own ones) do not follow this practice.