c# - 如何防止非法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(); }
}
解决方案
你不能重载一个类,所以:
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
是类的类型,如果不是则抛出异常,这可能会感觉像是编译时错误,如果它在运行时足够早地完成。
推荐阅读
- python - 在 python 中显示 sqlite3 数据
- android - OpenGL ES 2 不渲染任何东西
- android - 将字节码转换为dex时出错:多个dex文件定义了Landroid/arch/lifecycle/LiveData$LifecycleBoundObserver;
- php - 插入表 ps_order_payment prestashop
- embedded - CMSIS 风格的简单工作室中的 TX RX 测试代码
- vue.js - 如何将动态文本与 vuetify 面包屑一起使用
- assembly - 计算数字列表总和的程序
- c++ - 在 C++17 中引用 const 字符串的语义应该是什么?
- ios - 弹出视图推送当前视图 SwiftUI
- reactjs - 如何在reactjs中使用MDBSelect在下拉列表中使用“全选”选项?