Reputation: 5565
How can I get the type of generic parameter?
For example:
void Example<T>()
{
// Here I want to get the type of T (and how can I get if T is a primitive
// kind (int,bool,string) not class)
}
Upvotes: 4
Views: 488
Reputation: 27874
Type type = typeof(T);
That will get you the type object for type T.
type.IsPrimitive
will tell you if it's one of the primitive types, see list here: http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx
Also, note that although string
is a basic type, which is very integrated with the .NET system, it is not a primitive. System.String
is a full-fledged class, not a primitive.
Upvotes: 8
Reputation: 4012
Also you can get the type of T from an instance of the type T:
instance.GetType();
Upvotes: 2
Reputation: 74176
use the following for getting the type of T:
Type typeParameterType = typeof(T);
Upvotes: 6