Why can't I use constants in [SuppressMessage(…)] together with StyleCop?

不问归期 提交于 2020-01-15 03:11:14

问题


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?


回答1:


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.)



来源:https://stackoverflow.com/questions/33674077/why-cant-i-use-constants-in-suppressmessage-together-with-stylecop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!