假设我有几个派生类,其基类是泛型类。每个派生类都继承具有特定类型覆盖的基类(但所有类型也都派生自单个基类型)。
例如:
我有一个基行类
class RowBase
{
//some properties and abstract methods
}
我有两个从行基类派生的特定行类
class SpecificRow1 : RowBase
{
//some extra properties and overrides
}
class SpecificRow2 : RowBase
{
//some extra properties and overrides
}
然后我有第二个基类,它是一个泛型类,其中包含来自 RowBase 的派生类的集合
class SomeBase<T> where T : RowBase
{
ICollection<T> Collection { get; set; }
//some other properties and abstract methods
}
然后我有两个派生自 SomeBase 但使用不同的特定行类的类
class SomeClass1 : SomeBase<SpecificRow1>
{
//some properties and overrides
}
class SomeClass2 : SomeBase<SpecificRow2>
{
//some properties and overrides
}
现在,在我的主要或更大范围内,我想创建一个包含 SomeClass1 和 SomeClass2 对象的列表/集合。像
ICollection<???> CombinedCollection = new ...
CombinedCollection.Add(new SomeClass1())
CombinedCollection.Add(new SomeClass2())
.
.
.
//add more objects and do something about the collection
.
.
.
问题是:有可能有这样的收藏吗?如果可能的话,我怎样才能做到这一点?如果没有,还有什么替代方法?