简体   繁体   English

如何强制一个类实现从特定基类/接口(而不是特定类型)派生的属性

[英]How to force a class to implement a property that derives from a specific base class/interface (rather than is of a specific type)

In my solution, there are a number of classes C1 , C2 , C3 etc. that all inherit from a common abstract base class CB . 在我的解决方案中,有许多类C1C2C3等,它们都从通用抽象基类CB继承。

There are also a number of classes D1 , D2 , D3 etc. that act as a data-source for the corresponding C class (eg the data-source for C1 is a local property of type D1 etc). 还有许多类D1D2D3等,它们充当相应C类的数据源(例如, C1的数据源是D1等类型的局部属性)。 The D classes all inherit from a common abstract base class DB , but vary in their implementation (both inherited and non-inherited properties & methods are used by the C class). D类都继承自通用的抽象基类DB ,但是实现方式有所不同( C类使用继承的和非继承的属性和方法)。

Now, I want to impose a rule that all C classes (ie that derive from CB ) must implement a "data-source" property, and the type of this property must be derived from DB . 现在,我想强加一个规则,即所有C类(即从CB派生的)都必须实现“数据源”属性,并且该属性的类型必须DB派生。

My initial idea was to do this: 我最初的想法是这样做:

public abstract class CB
{
    protected abstract DB DataSource { get; set; } 

    etc.
}

However, this means that the overridden DataSource property in the C classes can only be of type DB , not a type derived from DB . 但是,这意味着C类中重写的DataSource属性只能是DB类型,而不是从DB派生的类型。

How can I impose my rule? 我该如何强加我的规则? Ideally CB and DB would remain abstract base classes (because there are non-abstract properties and methods in each that I wish the C and D classes to inherit), but they could be converted to interfaces if needed. 理想情况下, CBDB将保留为抽象基类(因为我希望CD类都继承非抽象的属性和方法),但是可以根据需要将它们转换为接口。 However I think I have exactly the same problem if I do that. 但是,我认为如果这样做,我会遇到完全相同的问题。

You're looking for generic class: 您正在寻找通用类:

public abstract class CB<T> where T : DB
{
    protected abstract T DataSource { get; set; } 

    etc
}

Now, C1 should be defined as: 现在, C1应该定义为:

public class C1 : CB<D1>
{
    protected override D1 DataSource { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个List实例,其类型派生自基类和接口 - Create an instance of a List where the type derives from a base class and an interface 如何使用受约束的泛型类型参数将 class 实例化为派生接口 - How to instantiate a class as the interface that it derives from with constrained generic type parameter 如何强制静态类实现特定方法? - How to force static class to implement specific methods? 如何确定一个类是否实现了特定的接口 - How to determine a class has implement a specific interface or not 确定对象是否来自特定类型? - Determine if object is or derives from specific Type? 为什么从比较器派生 <T> 类而不是实现IComparer的实现类 <T> 接口? - Why derive from Comparer<T> class rather than implement class that implements IComparer<T> interface? 从基类实例化特定类型的派生类属性 - Instantiate derived class properties of specific type from base class 实现一个泛型类,其中定义在基本接口类型上,但实现在从该基本接口派生的接口上? - Implement a generic class where the definition is on a base interface type but the implementation is on an interface derived from that base? 如何实现接口并从基类继承? - How can I implement an interface and inherit from a base class? 如何在C#中使用特定于派生类的枚举实现接口? - How to Implement Interface with Enum Specific to Derived Class in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM