我正在编写一个编写C#的程序,最终将其编译到应用程序中。我希望每个生成的类型都提供一个“深度克隆”功能,它复制整个数据树。也就是说,我希望有人能够做到:
var x = new Base(); // Base has public virtual Base DeepClone() { ... }
var y = new Derived(); // Derived overrides DeepClone
Base a = x.DeepClone();
Base b = y.DeepClone();
// Derived c = x.DeepClone(); // Should not compile
Derived d = y.DeepClone(); // Does not compile, DeepClone returns Base
而不是
var x = new Base();
var y = new Derived();
Base a = x.DeepClone();
Base b = y.DeepClone();
// Derived c = x.DeepClone(); // Should not compile
Derived d = (Derived)y.DeepClone();
但是,C#不允许您在简单的覆盖中执行此操作;覆盖必须返回与基础上声明的类型相同的类型。
由于我正在编写用于标记样板文件的代码,是否可以生成一些内容以允许第一个块进行编译?我尝试了类似以下内容:
abstract class Base
{
public abstract Base DeepClone();
}
class Base2 : Base
{
int Member { get; set; }
public Base2() { /* empty on purpose */ }
public Base2(Base2 other)
{
this.Member = other.Member;
}
public override Base2 DeepClone()
{
return new Base2(this);
}
}
sealed class Derived : Base2
{
string Member2 { get; set; }
public Derived() { /* empty on purpose */ }
public Derived(Derived other)
: base(other)
{
this.Member2 = other.Member2;
}
public override Derived DeepClone()
{
return new Derived(this);
}
}
但由于覆盖不匹配,因此无法编译。我也尝试从基础覆盖该方法并使用“new”关键字隐藏它,但这也不起作用。
答案 0 :(得分:4)
是的,它是可行的,但您必须将您的抽象方法从公共移动到受保护,然后创建一个只调用受保护方法的公共非抽象函数。派生类只需要实现受保护的函数,并且可以隐藏公共函数,执行客户端执行的转换。
abstract class Base
{
public Base DeepClone()
{
return CloneInternal();
}
protected abstract Base CloneInternal();
}
class Base2 : Base
{
int Member { get; set; }
public Base2() { /* empty on purpose */ }
public Base2(Base2 other)
{
this.Member = other.Member;
}
new public Base2 DeepClone()
{
return (Base2)CloneInternal();
}
protected override Base CloneInternal()
{
return new Base2(this);
}
}
sealed class Derived : Base2
{
string Member2 { get; set; }
public Derived() { /* empty on purpose */ }
public Derived(Derived other)
: base(other)
{
this.Member2 = other.Member2;
}
new public Derived DeepClone()
{
return (Derived)CloneInternal();
}
protected override Base CloneInternal()
{
return new Derived(this);
}
}
答案 1 :(得分:3)
这是一种不涉及任何演员的方法。它不允许你从你的第一个片段中做new Base()
,这没有任何意义,因为它是抽象的,但其余部分有效:
interface Base
{
Base DeepClone();
}
abstract class Base<T>: Base where T: Base<T>
{
public abstract T DeepClone();
Base Base.DeepClone() {
return DeepClone();
}
}
class Base2 : Base<Base2>
{
public override Base2 DeepClone()
{
return new Base2();
}
}
然后,在您的Main
方法中:
public static void Main()
{
var y = new Base2(); // Base2 overrides DeepClone
Base b = y.DeepClone();
Base2 c = y.DeepClone(); // Compiles an works
}