سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top