我有一个接口(interface) IArticle
有几个实现:ProductType
, RawMaterial
, PaintType
等
然后我有一个我认为是 IArticle
之间的复合元素的引用 (SKU)和一个 Color
:
public interface IReference
{
Color Color { get; set; }
IArticle Article { get; set; }
}
然后我有几个实现,每个都有对应的IArticle实现:
-
Product : IReference
会有一个ProductType
作为Article
, -
SemifinishedGood: IReference
会有一个RawMaterial
作为Article
-
Paint : IReference
会有一个PaintType
作为Article
所以问题是...我怎样才能覆盖 Article
输入,像这样:
public class Paint: IReference
{
public virtual Color Color { get; set; }
public virtual PaintType Article { get; set; }
}
这样我就可以访问PaintType
特定属性而不仅仅是 IArticle
当我处理 Paint.Article
时不必每次都投?这种架构有什么问题?
最佳答案
Generic type parameters很容易解决这个问题。
public interface IReference<TArticle> where TArticle : IArticle
{
Color Color { get; set; }
TArticle Article { get; set; }
}
public class Paint : IReference<PaintType>
{
public virtual Color Color { get; set; }
public virtual PaintType Article { get; set; }
}
关于c# - 接口(interface)实现覆盖另一个接口(interface)的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39450826/