发布于 2016-02-15 21:12:00
首先回答你的两个问题:
Object。若要确定类型是否为泛型,只需查看类定义即可。Queue不是泛型类型,而Queue<T>是泛型类型,因为可以通过将泛型类型参数T替换为任何其他类型来获得不同的类型。例如,Queue<int>、Queue<string>、Queue<Object>等都使用在Queue<T>类中定义一次的相同代码。注意,它们本身也是泛型类型。此外,嵌套在泛型类型中的嵌套类型也被视为泛型类型。“基本类型”Queue<T>被称为泛型类型定义。
public abstract class MyListBase { }
public abstract class MyListBase<T> : MyListBase { }
public class MyList<T> : MyListBase<T>
{
public class Nested { }
}
public class MyStringList : MyList<string> { }..。
var isGenericType0 = typeof(MyListBase).IsGenericType; //False
var isGenericType1 = typeof(MyListBase<>).IsGenericType; //True
var isGenericType2 = typeof(MyListBase<>)
.MakeGenericType(typeof(char)).IsGenericType; //True
var myIntegerList = new MyList<int>();
var isGenericType3 = myIntegerList.GetType().IsGenericType; //True
var myNested1 = new MyList<int>.Nested();
var isGenericType4 = myNested1.GetType().IsGenericType; //True
var myStringList = new MyStringList();
var isGenericType5 = myStringList.GetType().IsGenericType; //False希望你现在能把你的头转到通用类型上。
发布于 2016-02-15 08:29:12
泛型允许您将方法、类、结构或接口裁剪成它所操作的精确数据类型。
泛型类型定义是用作模板的类、结构或接口声明,它可以包含或使用类型的占位符。例如,System.Collections.Generic.Dictionary类可以包含两种类型:键和值。因为泛型类型定义只是模板,所以不能创建作为泛型类型定义的类、结构或接口的实例。
public class Generic<T>
{
public T Field;
}https://stackoverflow.com/questions/35404512
复制相似问题