How to pass a type to a method - Type argument vs generics

I believe you're misunderstanding what FxCop is telling you, probably because its wording is less than ideal. What it means is that a generic method should provide a parameter that is of that type, not that a generic method should have a non-generic overload that provides a runtime Type instance. For example,

public void DoSomething<T>(T myParam);

The myParam is the sort of parameter it's referring to. The reason it wants this is, as you suggest, for inference. This allows you to do something like...

string foo = "bar";

DoSomething(foo);

instead of having to write

DoSomething<string>(foo);

In your case, it's fine to suppress the warning since you want the user to explicitly specify the type. I would suggest, however (assuming that your constructors are parameterless) that you change your where to where T : SomeBaseClass, new(). This means that it will direct the compiler to require that whatever type is passed in have a parameterless constructor. This also means that you can do new T() in your code.


I would have no issue with suppressing this warning. For starters the equivalent in MS's own code is Activator.CreateInstance<T>()

public static T CreateInstance<T>()

It implies that the analysis rule should consider whether the return type of the method is covered by the generic parameter...

This has been mentioned in many places before:

  • CA1004 is not always appropriate
  • FxCop Microsoft.Design and Generic outputs

And there have been previous bugs in the rule for example:

public static void GenericMethod<T>(List<T> arg);

previously would trigger it (fixed in 2005 SP1).

I suggest filing a connect bug for your specific example


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.