试图抑制 StyleCop 消息 SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine

发布于 2024-08-29 02:57:13 字数 1592 浏览 4 评论 0原文

我试图抑制特定属性的以下 StyleCop 消息:

SA1513: Statements or elements wrapped in curly brackets must be followed by a blank line.

我正在尝试执行以下操作,但它似乎不起作用:

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]
    public string CustomerId
    {
        get
        {
            return this.GetProperty(CustomerIdProperty);
        }
        set
        {
            if (this.IsNew)
            {
                this.SetProperty(CustomerIdProperty, value);
            }
            else
            {
                throw new ReadOnlyException("Id value can only be changed for a new record.");
            }
        }
    }

我只是做错了什么吗?或者这是不可能的?这是一个很好的规则,只是对我的财产来说无效。

更新

尝试从 DocumentationRules 切换到 LayoutRules ...仍然没有抑制。

    [DataObjectField(true, false)]
    [SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]
    public string CustomerId
    {
        get
        {
            return this.GetProperty(CustomerIdProperty);
        }
        set
        {
            if (this.IsNew)
            {
                this.SetProperty(CustomerIdProperty, value);
            }
            else
            {
                throw new ReadOnlyException("Id value can only be changed for a new record.");
            }
        }
    }

I am trying to suppress the following StyleCop message for a specific property:

SA1513: Statements or elements wrapped in curly brackets must be followed by a blank line.

I am trying to do the following, but it doesn't seem to work:

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]
    public string CustomerId
    {
        get
        {
            return this.GetProperty(CustomerIdProperty);
        }
        set
        {
            if (this.IsNew)
            {
                this.SetProperty(CustomerIdProperty, value);
            }
            else
            {
                throw new ReadOnlyException("Id value can only be changed for a new record.");
            }
        }
    }

Am I just doing something wrong? Or is this just not possible? It's a good rule, just not valid in my case for a property.

Update

Tried switching from DocumentationRules to LayoutRules ... still not suppressing.

    [DataObjectField(true, false)]
    [SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]
    public string CustomerId
    {
        get
        {
            return this.GetProperty(CustomerIdProperty);
        }
        set
        {
            if (this.IsNew)
            {
                this.SetProperty(CustomerIdProperty, value);
            }
            else
            {
                throw new ReadOnlyException("Id value can only be changed for a new record.");
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

往日情怀 2024-09-05 02:57:13

我认为这可能是 StyleCop 的问题。您安装的是哪个版本? 此页面指出:

从 StyleCop 4.3.2 开始,可以通过在源代码中添加抑制属性来抑制规则违规的报告。

我刚刚发现我无法抑制任何消息。我使用的安装程序只提供了 4.3 版本。 Codeplex 上的最新版本是 4.4.0.0。确保您安装了该版本。

更新

我一直在做一些检查,我可以抑制 DocumentationRules:

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules",
                     "SA1600:ElementsMustBeDocumented",
                     Justification = "Reviewed. Suppression is OK here.")]

但不能抑制 SpacingRules 或 LayoutRules。然而,我没有发现任何证据表明为什么会出现这种情况。

I think this might be a problem with StyleCop. Which version do you have installed? This page states that:

Starting with StyleCop 4.3.2, it is possible to suppress the reporting of rule violations by adding suppression attributes within the source code.

I've just found that I can't suppress any messages. The installer I used just gives the version as 4.3. The latest version on the Codeplex is 4.4.0.0. Make sure you have that version installed.

Update

I've been doing some checking and I can suppress DocumentationRules:

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules",
                     "SA1600:ElementsMustBeDocumented",
                     Justification = "Reviewed. Suppression is OK here.")]

but not SpacingRules or LayoutRules. However, nothing I've found indicates why this should be the case.

凉城凉梦凉人心 2024-09-05 02:57:13

您的抑制使用 Microsoft.StyleCop.CSharp.DocumentationRules。我认为它应该是Microsoft.StyleCop.CSharp.LayoutRules

Your suppression uses Microsoft.StyleCop.CSharp.DocumentationRules. I think it should be Microsoft.StyleCop.CSharp.LayoutRules.

一指流沙 2024-09-05 02:57:13
[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]

适用于最新的 StyleCop。刚刚删除了“微软”。前缀。

[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine", Justification = "There are no issues with this code")]

works in latest StyleCop. Just removed "Microsoft." prefix.

镜花水月 2024-09-05 02:57:13

StyleCop 中有一个错误,让您只能抑制某些类型的规则。该问题将在即将发布的 StyleCop 4.4 中得到修复。

There is a bug in StyleCop that let's you only suppress certain kinds of rules. This will be fixed in StyleCop 4.4, which is due to be released soon.

屋顶上的小猫咪 2024-09-05 02:57:13

请仔细阅读 StyleCop 文档以了解如何抑制规则。以下内容在我的代码中起作用:

    [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules",
    "SA1402:FileMayOnlyContainASingleClass",
    Justification = "Splitting this file into classes would get too confusing.")]

来自帮助文件:

SuppressMessage 属性具有以下格式:

[SuppressMessage("规则类别、"规则 ID"、"理由")]

地点:

  • 规则类别 - 在其中定义规则的 StyleCop 规则命名空间。例如,
    Microsoft.StyleCop.CSharp.DocumentationRules

  • 规则 ID - 规则的标识符,使用格式短名称:长名称。

    例如,SA1600:ElementsMustBeDocumented

  • 理由 - 用于记录禁止原因的文本
    消息。

正如已经提到的,确保您引用正确的规则命名空间。

Be careful to read the StyleCop documentation to figure-out how to suppress a rule. The following worked in my code:

    [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules",
    "SA1402:FileMayOnlyContainASingleClass",
    Justification = "Splitting this file into classes would get too confusing.")]

From the help file:

The SuppressMessage attribute has the following format:

[SuppressMessage("Rule Category, "Rule Id", "Justification")]

Where:

  • Rule Category - The StyleCop rule namespace in which the rule is defined. For example,
    Microsoft.StyleCop.CSharp.DocumentationRules

  • Rule Id -The identifier for the rule, using the format shortname:longname.

    For example, SA1600:ElementsMustBeDocumented

  • Justification - The text that is used to document the reason for suppressing
    the message.

And as already mentioned, make sure you are referencing the correct rules namespace.

勿忘心安 2024-09-05 02:57:13

只需在 get 块和 set 块之间添加一个空行即可。
这就是您所要做的,添加一个空行,问题就解决了。

Just put a blank line between your get block and your set block.
That's all you have to do, add one blank line and the problem is solved.

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