2010-04-10 70 views
4

我试图压制下了StyleCop消息特定属性:试图压制了StyleCop消息SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine

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."); 
      } 
     } 
    } 

回答

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。但是,我没有发现任何内容,表明为什么会这样。

+0

我认为这可能是一个错误...试图抑制该消息仍然给出警告。 – mattruma 2010-04-10 16:29:03

+0

@mattruma - 我认为在这种情况下规则名称是正确的 – ChrisF 2010-04-10 16:42:23

+0

我同意ChrisF的评估 - 即使在4.4中,SpacingRules和LayoutRules似乎也不能被抑制。 – Joe 2011-02-10 15:03:30

3

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

+0

这是有道理的......做出了改变,但仍然没有压制。 – mattruma 2010-04-10 15:52:23

2

StyleCop有一个错误,让你只能抑制某些类型的规则。这将在StyleCop 4.4中得到解决,该版本即将发布。

-1

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

+3

-1这篇文章不回答OP的问题。他问如何禁用该规则,而不是如何遵守该规则。 – 2013-04-17 10:22:59

1

请仔细阅读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

  • 规则标识 -The标识符规则,使用格式短名称:LONGNAME。

    例如,SA1600:ElementsMustBeDocumented

  • 正当性 - 这是用来记录用于抑制 该消息的原因的文本。

而且如前所述,请确保您引用正确的规则命名空间。

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

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