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)
}
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.
use the following for getting the type of T:
Type typeParameterType = typeof(T);
您还可以从类型T的实例中获取T的类型:
instance.GetType();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.