通用ICloneable<T>不存在是否有特殊原因?
如果我不需要每次克隆时都强制施放它,那就舒服多了。
通用ICloneable<T>不存在是否有特殊原因?
如果我不需要每次克隆时都强制施放它,那就舒服多了。
当前回答
ICloneable现在被认为是一个糟糕的API,因为它没有指定结果是深度复制还是浅复制。我认为这就是为什么他们不改进这个界面的原因。
您可能可以使用类型化克隆扩展方法,但我认为它需要一个不同的名称,因为扩展方法的优先级比原始方法低。
其他回答
ICloneable现在被认为是一个糟糕的API,因为它没有指定结果是深度复制还是浅复制。我认为这就是为什么他们不改进这个界面的原因。
您可能可以使用类型化克隆扩展方法,但我认为它需要一个不同的名称,因为扩展方法的优先级比原始方法低。
如果你需要的话,自己编写接口是很容易的:
public interface ICloneable<T> : ICloneable
where T : ICloneable<T>
{
new T Clone();
}
I need to ask, what exactly would you do with the interface other than implement it? Interfaces are typically only useful when you cast to it (ie does this class support 'IBar'), or have parameters or setters that take it (ie i take an 'IBar'). With ICloneable - we went through the entire Framework and failed to find a single usage anywhere that was something other than an implementation of it. We've also failed to find any usage in the 'real world' that also does something other than implement it (in the ~60,000 apps that we have access to).
现在,如果你只是想强制一个你想要你的“可克隆”对象实现的模式,这是一个完全好的用法——去吧。你也可以决定“克隆”对你来说到底意味着什么(即深度克隆还是浅克隆)。然而,在这种情况下,我们(BCL)不需要定义它。只有当需要在不相关的库之间交换抽象类型的实例时,我们才在BCL中定义抽象。
大卫·基恩(BCL队)
除了Andrey的回复(我同意,+1)-当ICloneable完成时,你还可以选择显式实现,使公共Clone()返回一个类型化对象:
public Foo Clone() { /* your code */ }
object ICloneable.Clone() {return Clone();}
当然,泛型ICloneable<T> -继承还有第二个问题。
如果我有:
public class Foo {}
public class Bar : Foo {}
我实现了ICloneable<T>,那么我实现了ICloneable<Foo>吗?酒吧ICloneable < > ?你很快就开始实现许多相同的接口…… 与演员相比……真的有那么糟糕吗?
我认为“为什么”这个问题是不必要的。有很多接口/类等…它非常有用,但不是. net Frameworku基础库的一部分。
但是,主要是你可以自己做。
public interface ICloneable<T> : ICloneable {
new T Clone();
}
public abstract class CloneableBase<T> : ICloneable<T> where T : CloneableBase<T> {
public abstract T Clone();
object ICloneable.Clone() => return this.Clone();
}
public abstract class CloneableExBase<T> : CloneableBase<T> where T : CloneableExBase<T> {
protected abstract T CreateClone();
protected abstract void FillClone(T clone);
public override T Clone() {
T clone = this.CreateClone();
if (clone is null ) {
throw new NullReferenceException( "Clone was not created." );
}
this.FillClone(clone);
return clone
}
}
public abstract class PersonBase<T> : CloneableExBase<T> where T : PersonBase<T> {
public string Name { get; set; }
protected override void FillClone( T clone ) {
clone.Name = this.Name;
}
}
public sealed class Person : PersonBase<Person> {
protected override Person CreateClone() => return new Person();
}
public abstract class EmployeeBase<T> : PersonBase<T> where T : EmployeeBase<T> {
public string Department { get; set; }
protected override void FillClone(T clone) {
base.FillClone(clone);
clone.Department = this.Department;
}
}
public sealed class Employee : EmployeeBase<Employee> {
protected override Employee CreateClone() => return new Employee();
}