问题描述
可能重复:结果
的如何创建一个从相同的通用类继承了不同类型的对象的列表?一>
我使用的,他们是从一个抽象类继承而来的几个对象。但是使用抽象类必须是一个独立宣言通用的数据类型。
I'm using several objects where they are inherited from an abstract class. But to use the abstract class must be declara a generic datatype.
我有问题,因为我需要在这里包含ProblemBase的列表清单,虽然每一个。包含不同的数据类型TResult
I'm having problems because I need to have a list where contains a list of ProblemBase, although each one contains a different TResult datatype.
public abstract class ProblemBase<TResult>
{
TResult[] Array;
}
和我想要得到Array属性。这就是问题所在。
And I want to get Array property. That's the problem.
推荐答案
这类型的事情发生,我经常。该解决方案我通常一起去是有 ProblemBase℃的基类; T>
是类型分类:
This type of thing happens for me quite often. The solution I typically go with is to have a base class for ProblemBase<T>
that is type free:
public abstract class ProblemBase
{
public abstract object Result { get; }
}
public abstract class ProblemBase<TResult> : ProblemBase
{
public override object Result
{
get { return Result; }
}
new public TResult Result { get; private set; }
}
当你需要的问题的集合,那么,你可以做一个集合的 ProblemBase
没有泛型。
如果 TResult
有其自身需要的继承层次结构,那么你可以这样做,而不是:
If TResult
has its own required inheritance hierarchy, then you can do this instead:
public abstract class ProblemBase
{
public abstract ResultBase Result { get; }
}
public abstract class ProblemBase<TResult> : ProblemBase
where TResult : ResultBase
{
public override ResultBase Result { get { return Result; } }
new public TResult Result { get; private set; }
}
这篇关于如何有ProblemBase℃的列表; TResult> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!