C#泛型学习02--获取泛型的类型

一、typeof获取泛型类型

View Code
public class SimpleGeneric<T>
{
public SimpleGeneric() { }
}

 

View Code
public static void Main(string[] args)
{
Type gtInt = typeof(SimpleGeneric<int>);
Type gtBool = typeof(SimpleGeneric<bool>);
Type gtString = typeof(SimpleGeneric<string>);
}

在调用typeof时类型参数必须要提供

运行结果如下:

 

二、GetType()获取泛型类型

View Code
public class SimpleGeneric<T>
{
public SimpleGeneric() { }
}

 

View Code
public static void Main(string[] args)
{
SimpleGeneric<int> sgI = new SimpleGeneric<int>();
Type alsoGT = sgI.GetType();

Console.WriteLine(alsoGT);
}

运行结果如下:



 

posted @ 2012-03-01 23:30  chen3jian  阅读(1798)  评论(0编辑  收藏  举报