I have a method of an object which is something like a factory. You give it a type, it creates an instance and does a few other things. An elegant way to do it (in my opinion) is like this:
public T MagicMethod<T>() where T: SomeBaseClass
{
// Magic goes here
}
But this upsets FxCop who says that this is a bad style – I get a “CA1004: Generic methods should provide type parameter” warning. Something about not being to use inference and stuff. So, the only other way I can think of is something like this:
public SomeBaseClass MagicMethod(Type T)
{
// Same magic goes here
}
I believe this is inferior to the first method on many accounts, but the style rule… The MSDN article on the warning even says that there is no reason for suppressing it.
Am I doing it right by suppressing this warning after all?
FXCop warnings are just that – warnings. Just like implicit cast warnings, they serve to let you know that something you’re doing may have behavior you’re not anticipating, or may not be what you intended.
An implicit cast warning is dealt with by looking at the code, determinining if you really did intend to do that, and if so, adding an explicit cast.
Same thing with FXCop. Look at the warning, look at your code, and determine if the warning is valid. If it is, fix it. If not, suppress it. A suppression is the equivalent of an explicit cast – “Yes, FXCop, I’m sure I want to do this.”
If it was really truly an error, it would probably be a compiler error.