将接口上的泛型参数限制为子类

时间:2012-07-19 21:49:46

标签: c# generics parameters restrictions

以下是做作的,但请耐心等待:

interface Clonable<TSubClass>
{
    TSubClass Clone();
}

如何将TSubClass限制为实现类型?

即只允许实现者这样做:

class Dog : Clonable<Dog>
{
    Dog Clone() 
    {
        ....
    }
}

不是这个:

class BadDog : Clonable<Rabbit>
{
    Rabbit Clone()
    {
        ....
    }
}

2 个答案:

答案 0 :(得分:8)

can't只能通过惯例和文件来强制执行....

我的惯例是使用像TSelf这样的东西。

interface ICloneable<TSelf> where TSelf : ICloneable<TSelf> { ... }

另请注意,实现或继承此接口的任何非具体构造都应通过...

传递约束
[Serializable]
abstract class SerializableCloneable<TSelf> : ICloneable<TSelf> 
  where TSelf : SerializableCloneable<TSelf> { ... }

注意:我已使用调用NRoles的惯例在self-type parameter S中实施了此项检查。

答案 1 :(得分:3)

您无法在编译时强制执行此操作,因为.NET泛型没有模板特化或鸭子类型。

但是,您可以包含一个静态构造函数(类型初始化程序),它使用反射在加载时断言关系。好的,C#不允许您在接口上放置静态构造函数(即使.NET允许它),因此您需要使用模块初始化程序或您自己调用的函数。此外,您还需要搜索实现该接口的类型,包括尚未加载的类型(您可以订阅Assembly.Load事件以通知将来加载的类型。)

相关问题