public interface IHasFeature<TFeature> {
TFeature Feature { get; set; }
}
public class FeatureOne {
/*...*/
}
public class ProductOne : IHasFeature<FeatureOne> {
public FeatureOne Feature { get; set; }
}
public abstract class BaseContainer<TProduct, TFeature>
where TProduct : IHasFeature<TFeature> {
public TProduct Product { get; set; }
public void DoProcess() {
var result = Product.Feature.Execute(); //Execute is an extension method
}
}
public class MyContainer : BaseContainer<ProductOne, FeatureOne> {
/*...*/
}
当我这样做时工作:
MyContainer : BaseContainer<ProductOne, FeatureOne>
但我想:
MyContainer : BaseContainer<ProductOne>
ProductOne : IHasFeature<...>
应该已经包含嵌套的通用特性TFeature
,我不想在MyContainer
构造中再次重复它们。
知道如何改进吗?谢谢。
EDIT2 ----------
删除了 new 关键字,正如 Nenad 所说的那样是错误的。