首页 > 解决方案 > 如何防止非法ICloneableC#中的继承?

问题描述

我有一个界面:

public interface ICloneable<out T>
    where T : ICloneable<T>
{
    T Clone();
}

应该接收实现此接口的类型(如下所示)。
我可以创建一个实现它的类:

public class Class : ICloneable<Class>
{
    public Class Clone() { return (Class)MemberwiseClone(); }
}

伟大的 !

但是任何人都可以创建一个实现 ICloneable<T> “错误”的类。
是否存在防止继承的方法,如下所示?(2 个例子)

public class Other : ICloneable<Class>
{
    public Class Clone() { return new Class(); }
}

public class Other : Class, ICloneable<Class>
{
    public Class Clone() { return (Other)MemberwiseClone(); }
}

并允许继承,如下所示?(来自 2 个示例)

public class Other : ICloneable<Other>
{
    public Other Clone() { return (Other)MemberwiseClone(); }
}

public class Other : Class, ICloneable<Other>
{
    public Other Clone() { return (Other)MemberwiseClone(); }
}

标签: c#oopgenericsinheritanceinheritance-prevention

解决方案


你不能重载一个类,所以:

public class Other : Class {}
public class Other : Class, IC<Other> {}

永远不会工作。

现在,我要拉一个 Jon Skeet 并展示你如何做到这一点,然后阻止你这样做。你可以这样做:

public class CloneableOther : Class, ICloneable<Other> {  }
public class Other : CloneableOther
{

}    

public class CloneableFoo : Class, ICloneable<Foo> { }
public class Foo : CloneableFoo
{

}

这段代码正在做的是有效地从继承中删除泛型参数。除了,Foo仍然可以这样做:Foo : CloneableFoo, ICloneable<Other>,现在您必须为每个ICloneable实例创建两个类。

这就是为什么你首先需要这个?这是一种做法Foo : IInterface<Foo>,但没有办法强制执行。你最好的选择是复制和粘贴,并确保类匹配。

也许另一种方法是在构造函数中Class检查类型是否ICloneable是类的类型,如果不是则抛出异常,这可能会感觉像是编译时错误,如果它在运行时足够早地完成。


推荐阅读