citelao
citelao

Reputation: 6046

What is required to include in a SuppressMessage attribute?

I have some code that violates, say, CA1051:

public class Logger {
    // Generates a warning "Do not declare visible instance fields".
    public Level LogLevel = Level.Warning;
}

I want to suppress this warning, since I'm breaking that rule deliberately. MSDN says to use the SuppressMessage attribute to do so:

[SuppressMessage("Design", "CA1051: Do not declare visible instance fields", Justification = "Clearest way of exposing this field.")]
public Level LogLevel = Level.Warning;

But all the examples imply that:

Is there any simpler way of suppressing the error/warning?

Disclaimer: I work for Microsoft.

Upvotes: 5

Views: 1203

Answers (1)

citelao
citelao

Reputation: 6046

Yes, there is a simpler way of suppressing errors/warnings like this!

// 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;

It seems undocumented, but the ID is sufficient to suppress the message. I recommend adding a Justification since that helps other readers of the code understand why you chose to break the rule.

Also note that Visual Studio proper will help you generate these suppression attributes automatically. VS Code just doesn't seem to.

Upvotes: 5

Related Questions