tags:

views:

553

answers:

1

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?

+8  A: 

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.

FinnNk
Thanks! This should work, though I was hoping for a simpler approach. Was hoping for an method where I can simply say "ignore StyleCop for these lines" - instead of having to find the correct text for style names etc. Awaiting to see if this is possible.
stiank81
AFAIK it's not possible - the ability to suppress messages is itself a fairly recent addition.
FinnNk
Thanks. You're probably right then, but I'm gonna leave the question out there for a little longer..
stiank81
Well - no new answers for a week, so assume my wish isn't achievable yet then.. Thanks for your answer!
stiank81