我有一些代码违反了,比如说,CA1051
public class Logger {
// Generates a warning "Do not declare visible instance fields".
public Level LogLevel = Level.Warning;
}我想压制这个警告,因为我是故意违反规则的。MSDN说使用SuppressMessage属性这样做:
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields", Justification = "Clearest way of exposing this field.")]
public Level LogLevel = Level.Warning;但所有的例子暗示:
是否有更简单的方法来抑制错误/警告?
发布于 2021-12-28 18:34:30
是的,有一种更简单的方法来抑制这样的错误/警告!
// Minimal required:
[SuppressMessage("", "CA1051")]
public Level LogLevel = Level.Warning;
// What I recommend
[SuppressMessage("", "CA1051", Justification = "Clearest way of exposing this field")]
public Level LogLevel = Level.Warning;它似乎没有文档,但ID足以阻止消息。我建议添加一个Justification,因为这可以帮助代码的其他读者理解为什么您选择违反规则。
还请注意,Visual将帮助您自动生成这些抑制属性。与代码似乎不一样。
https://stackoverflow.com/questions/70511134
复制相似问题