Preventing multiple reporting of the same rule violation in FxCop -- What is Id?

727 views Asked by At

FxCop is currently reporting the same rule violation for a particular method -- it has two out parameters, because I want to return two values to the caller without creating a struct for it. I wonder if anonymous types would solve my problem, but I didn't know about them at the time I had written the method.

Anyhow, I'm getting CheckId CA1021 reported once for each parameter. I've copied the SuppressMessage text from FxCop, and then realized that the Id for each message is different! To me, it seems like you only need the CheckId, so...

  1. what is the Id used for? I haven't been able to find information about it online.
  2. will the Id remain the same? I assume so, or SuppressMessage wouldn't work the way one would want it to
  3. is there a way to specify the SuppressMessage attribute so that it suppresses for all Ids?
1

There are 1 answers

0
Guildencrantz On BEST ANSWER

From In Source Suppression Overview

Attributes can only be applied to a method and cannot be embedded within the method body. However, you can specify the identifier as the message ID to distinguish multiple occurrences of a violation within a method.

In other words: The MessageId is used to suppress a single instance of the error within the scope; this is handy if you're worried about accidentally introducing the error again within the same method and want to catch that new introduction with FxCop (or if you want to specify different justifications for each occurrence of the issue). If you aren't afraid of introducing more of these errors (or don't care if you do), and have the same justification, you can simply leave off the MessageId in the Suppress statement: [SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters")].