Disable StyleCop for specific lines

后端 未结 5 636
無奈伤痛
無奈伤痛 2021-02-05 01:12

We\'re using StyleCop in our C# projects. In some cases we\'d like to avoid the rules though. I know you can add // in the beginning of the

相关标签:
5条回答
  • 2021-02-05 01:37

    Decorate your class or method with the following StyleCop attribute:

    [GeneratedCode("Tool Goes Here", "Message Goes Here")]
    
    0 讨论(0)
  • 2021-02-05 01:38

    An old question I know but in looking for an answer I found that in stylecop 4.4 you can now put something like this - or one of these lines on a method:

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "*", Justification = "Risky to change manually")]
    [SuppressMessage("Microsoft.StyleCop.CSharp.SpacingRules", "*", Justification = "Risky to change manually")]
    [SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "*", Justification = "Risky to change manually")]
    [SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "*", Justification = "Risky to change manually")]
    [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "*", Justification = "Risky to change manually")]
    [SuppressMessage("Microsoft.StyleCop.CSharp.ReadabilityRules‌​", "*", Justification = "Risky to change manually")]
    [SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "*", Justification = "Risky to change manually")]
    
    • Note: I might be missing one or two of the rule categories
    0 讨论(0)
  • 2021-02-05 01:51

    This guy seems to have a good general ignore hack; he suggests putting this at the top of the file - tested and working with R#

    //------------------------------------------------------------------------------
    // <auto-generated>
    // This code was generated by a tool.
    // </auto-generated>
    //------------------------------------------------------------------------------
    

    Handy for when you are just churning out a load of boilerplate to adhere to a mainly unimplemented interface, for example.

    0 讨论(0)
  • 2021-02-05 01:52

    You can suppress rules by adding attributes to blocks of code. Here's a simple example on a class from the blog post linked below, but you can do it on various members individually:

    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented")]
    public class MyUndocumentedClass
    {
        public void MyUndocumentedMethod {}
    } 
    

    There's a quick overview at an MSDN blog post and a fuller description of the attributes on MSDN.

    0 讨论(0)
  • 2021-02-05 01:59

    You can put the piece of code between a region name Generated Code and it will be ommited.

    0 讨论(0)
提交回复
热议问题