让您的泛型类从非泛型基类继承,或实现一个非泛型接口。然后,您可以拥有此类型的集合,并将其转换为用于访问集合内容的任何代码。
这是一个例子。
public abstract class MyClass{ public abstract Type Type { get; }}public class MyClass<T> : MyClass{ public override Type Type { get { return typeof(T); } } public T Value { get; set; }}// VERY basic illustration of how you might construct a collection// of MyClass<T> objects.public class MyClassCollection{ private Dictionary<Type, MyClass> _dictionary; public MyClassCollection() { _dictionary = new Dictionary<Type, MyClass>(); } public void Put<T>(MyClass<T> item) { _dictionary[typeof(T)] = item; } public MyClass<T> Get<T>() { return _dictionary[typeof(T)] as MyClass<T>; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)