I would like to place the strings needed for suppression of StyleCop warnings as constants in a class, so that I do not need to place strings all over and benefit from the find all references function to find out how many times I have suppressed which rule.
public class Rules
{
public const string Naming = "Microsoft.StyleCop.CSharp.NamingRules";
public const string SA1310 = "SA1310:FieldNamesMustNotContainUnderscore";
}
Decorating my class as follows
[SuppressMessage(Rules.Naming, Rules.SA1310)]
public class MyClass
{
public readonly int my_field;
}
makes StyleCop still complain about fields whose names contain underscores. Only when attributing the class with
[SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore")]
the StyleCop warnings disappear. Why is that? Is StyleCop parsing directly my source code? Is there are way to make it work in the manor stated above?
StyleCop works against source code, not compiled assemblies. There is nothing in the StyleCop logic for consuming SuppressMessageAttribute
instances that would attempt to dereference constants to read their values, so there is essentially nothing you can do to make StyleCop recognize your constants. (If you've been using this approach successfully with FxCop, it works because FxCop analyzes the compiled assemblies, where the references to the constants have already been replaced by their literal values.)