How to get the type of a universal parameter?

How can I get the type of the general parameter?

For instance:

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) } 
+4
source share
3 answers
 Type type = typeof(T); 

This will give you a type object for type T.

type.IsPrimitive will tell you if it is one of the primitive types, see here: http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx

Also note that although string is a basic type that is very integrated with the .NET system, it is not primitive. System.String is a full-fledged class, not a primitive.

+8
source

use to get type T:

 Type typeParameterType = typeof(T); 

typeof (link to C #)

+6
source

You can also get type T from an instance of type T:

 instance.GetType(); 
+2
source

All Articles