试图压制StyleCop消息SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine

时间:2010-04-10 15:38:56

标签: c# stylecop

我正在尝试为特定属性抑制以下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.");
            }
        }
    }

6 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

我认为这可能是StyleCop的一个问题。你安装了哪个版本? This page声明:

  

从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。但是,我发现的任何内容都没有说明为什么会出现这种情况。

答案 2 :(得分:2)

StyleCop中存在一个错误,让您只能抑制某些规则。这将在StyleCop 4.4中修复,该版本即将发布。

答案 3 :(得分:2)

小心阅读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

  •   
  • 规则标识 - 规则的标识符,使用格式shortname:longname。

         

    例如,SA1600:ElementsMustBeDocumented

  •   
  • 对齐 - 用于记录抑制原因的文本   消息。

  •   

如前所述,请确保引用正确的规则名称空间。

答案 4 :(得分:2)

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

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

答案 5 :(得分:-2)

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

相关问题