【发布时间】:2018-12-22 04:16:50
【问题描述】:
我有一个这样的通用接口:
public interface IHardwareProperty<T>{
bool Read();
bool Write();
}
这是“通过”一个抽象基类:
public abstract class HardwareProperty<T>:IHardwareProperty<T>{
//Paritally implements IHardwareProperty (NOT SHOWN), and passes Read and Write through
//as abstract members.
public abstract bool Read();
public abstract bool Write();
}
并在几个使用不同泛型参数的继承类中完成
CompletePropertyA:IHardwareProperty<int>
{
//Implements Read() & Write() here
}
CompletePropertyBL:IHardwareProperty<bool>
{
//Implements Read() & Write() here
}
我想在同一个集合中存储一堆不同类型的完整属性。
有没有办法做到这一点而不求助于收集objects?
【问题讨论】:
-
我在这里看到的完成您想要的唯一方法是添加一个非通用接口作为通用接口的基础,例如
public interface IHardwareProperty<T> : IHardwareProperty然后存储基数:List<IHardwareProperty> propertyList... -
为什么你的界面首先是通用的?你从不使用
T... -
谢谢,我实际上是在发帖几分钟后才知道的。我想输入问题帮助我解决了问题。我改变了它,所以 IHardwareProperty 有一个
object GetValue()方法。它不太好,因为当我通过它访问值时我仍然需要强制转换它,但它完成了工作。
标签: c# generics inheritance