Disable StyleCop for specific lines

stiank81 picture stiank81 · Nov 18, 2009 · Viewed 17.6k times · Source

We're using StyleCop in our C# projects. In some cases we'd like to avoid the rules though. I know you can add // <auto-generated /> in the beginning of the file to make StyleCop ignore it. However, I don't want to ignore the rules for the whole file - only a block of code within it.

Can I disable StyleCop for specific lines somehow?

Answer

FinnNk picture FinnNk · Nov 18, 2009

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.