StyleCop 和 FxCop 规则相互排除

发布于 2024-08-04 07:04:43 字数 906 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你丑哭了我 2024-08-11 07:04:43

使用完整的属性名称:

[System.CLSCompliant(true)]
namespace MyNamespace
{
    ...
}

顺便说一句:如果要将整个程序集标记为 CLSCompliant,请放入

[assembly: System.CLSCompliant(true)]

Properties/AssemblyInfo.cs 文件

Use full attribute name:

[System.CLSCompliant(true)]
namespace MyNamespace
{
    ...
}

BTW: if you want to mark your whole assembly as CLSCompliant, put

[assembly: System.CLSCompliant(true)]

in Properties/AssemblyInfo.cs file

怂人 2024-08-11 07:04:43

我的建议是关闭“所有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文