1

c#/.net/fxcop!

... within a class, I want FxCop to shut up complaining about

Warning CA1062 : Microsoft.Design : 
In externally visible method 'xyz', validate parameter 'a' before using it. 

Basically, this rule suggests that I put a if (a == null) throw new ArgumentNullException("a"); at the start of almost every method in my code. This sucks and changes the exception handling logic.

So, I put this somewhere into my class body:

[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods",
     Scope = "Type", 
     MessageId = "0", 
     Justification = "We love danger... so far.")]

Anyway, this does not even suppress a single message - I still get all these warnings. It only suppresses the warning if it stands right above one of the fallible method definitions (i.e. it only supresses one single warning, not all of this type). The odd thing is that the same syntax works for other issues that occur multiple times in my class.

I don't know what I'm doing wrong, and I frankly don't really understand how to use the attribute parameters.. http://msdn.microsoft.com/de-de/library/ms244717.aspx doesn't go too much into details. :T ... anyway, any ideas what's not right?

Efrain
  • 3,248
  • 4
  • 34
  • 61
  • While this isn't a duplicate, it might also solve your problem: http://stackoverflow.com/questions/35551/excluding-fxcop-rule-in-source Check out the answer there, I bet it helps – taylonr Apr 27 '11 at 15:23
  • Why dont you supress those warnings through your Project properties settings. –  May 29 '13 at 10:34

1 Answers1

4

Do you want to suppress the message for all classes or only a single class? If the former, you can disable the rule. (The exact mechanism for doing so depends on how you're running FxCop, so please provide details if you need help with this.)

If the latter, you're going to need to add a SuppressMessage attribute for at least each method in which the problem is detected. The reason for this is that FxCop only looks for suppressions on the target of a violation. A suppression added to a "parent" element (e.g.: the class to which a method belongs) is never examined by FxCop when attempting to determine whether a violation has been suppressed.

You do have some flexibility with respect to where you place the suppression in your code if you use the Target attribute, but this does not change the need for one suppression attribute per violation target.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • Thanks! And well.. that's quite lame, because my class is a data type with about 50 methods. I need to place them in front of each method definition. :T ... it's c#; all the functions I use are from .net and will actually throw proper exceptions themselves. So yeah, since I think little of this rule anyway, I'll propbably disable the rule solution-wide. – Efrain Apr 28 '11 at 07:07