4
说我有几个基类是泛型类的派生类。每个派生类都使用特定的类型覆盖继承基类(但所有类型也都是从单个基类型派生的)。具有泛型基类的派生类的集合
例如:
我有一个基行类
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
.
.
.
问题是:有可能有这样的收藏?如果有可能,我该如何做到这一点?如果不是,可以采用哪种替代方法?
啊,接口!这就是我想念的。谢谢! – noddy
这可能已经超出我的问题,但如果我有方法具有泛型类型T的参数,我想在循环myList集合时调用它。在这种情况下,我不能声明T型协变。那是否意味着我根本无法做到这一点? – noddy