问题描述
说我有几个派生类的基类是一个通用类。每个派生类继承具有特定类型覆盖的基类(但是所有类型也都派生自单个基本类型)。
例如:
我有一个基本行类
class RowBase
{
//一些属性和抽象方法
}
派生自行基类
class SpecificRow1:RowBase
{
//一些额外的属性和覆盖
}
class SpecificRow2:RowBase
{
//一些额外的属性和覆盖
}
然后我有一个第二个基类是一个泛型类,它包含一个来自RowBase派生类的集合
class SomeBase< T>其中T:RowBase
{
ICollection< T> Collection {get;组; }
//其他一些属性和抽象方法
}
从SomeBase派生但是使用不同的特定行类的两个类
class SomeClass1:SomeBase< SpecificRow1&
{
//一些属性和覆盖
}
class SomeClass2:SomeBase< SpecificRow2>
{
//一些属性和覆盖
}
在我的主要或更大的范围,我想创建一个包括SomeClass1和SomeClass2对象的列表/集合。喜欢
ICollection< CombinedCollection = new ...
CombinedCollection.Add(new SomeClass1())
CombinedCollection.Add(new SomeClass2())
。
。
。
//添加更多对象并对集合
做一些事情。
。
。
问题是:有可能有这样的收集吗?如果可能,我该如何实现呢?如果没有,什么可以替代方法?
这可以通过。
添加一个新接口,并使T参数协变(使用 out 关键字):
interface ISomeRow< out T>其中T:RowBase
{
}
SomeBase应该继承该接口:
class SomeBase< T> :ISomeRow< T>其中T:RowBase
{
//一些其他属性和抽象方法
}
然后,以下将工作:
List< ISomeRow< RowBase> myList = new List< ISomeRow< RowBase>>();
myList.Add(new SomeClass1());
myList.Add(new SomeClass2());希望这是您要查找的内容:)
Say I have several derived classes whose base class is a generic class. Each derived class inherit the base class with a specific type override(but all types are also derived from a single base type).
For example:
I have a base row class
class RowBase
{
//some properties and abstract methods
}
And I have two specific row classes that are derived from the row base class
class SpecificRow1 : RowBase
{
//some extra properties and overrides
}
class SpecificRow2 : RowBase
{
//some extra properties and overrides
}
Then I have a second base class that is a generic class which contains a collection of derived classes from RowBase
class SomeBase<T> where T : RowBase
{
ICollection<T> Collection { get; set; }
//some other properties and abstract methods
}
Then I have two classes that derive from SomeBase but are using different specific row class
class SomeClass1 : SomeBase<SpecificRow1>
{
//some properties and overrides
}
class SomeClass2 : SomeBase<SpecificRow2>
{
//some properties and overrides
}
Now that in my main or a bigger scope, I want to create a list/collection that consist both SomeClass1 and SomeClass2 objects. Like
ICollection<???> CombinedCollection = new ...
CombinedCollection.Add(new SomeClass1())
CombinedCollection.Add(new SomeClass2())
.
.
.
//add more objects and do something about the collection
.
.
.
The question is: is it possible to have such collection? If it is possible, how can I achieve this? If no, what can be an alternative way?
解决方案 This can be done with the help of Covariance and Contravariance.
Add a new interface that and make the T parameter covariant (using the out keyword):
interface ISomeRow<out T> where T : RowBase
{
}
SomeBase should inherit that interface like this:
class SomeBase<T> : ISomeRow<T> where T : RowBase
{
//some other properties and abstract methods
}
Then, the following will work:
List<ISomeRow<RowBase>> myList = new List<ISomeRow<RowBase>>();
myList.Add(new SomeClass1());
myList.Add(new SomeClass2());
Hope this is what you're looking for :)
这篇关于具有通用基类的派生类的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!