本文介绍了我需要实现C#深拷贝构造函数与继承。是什么模式有可供选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我在C#中的类层次结构深度拷

I wish to implement a deepcopy of my classes hierarchy in C#

public Class ParentObj : ICloneable
{
    protected int   myA;
    public virtual Object Clone ()
        {
             ParentObj newObj = new ParentObj();
             newObj.myA = theObj.MyA;
             return newObj;
        }
}

public Class ChildObj : ParentObj
{
    protected int   myB;
    public override Object Clone ( )
        {
             Parent newObj = this.base.Clone();
             newObj.myB = theObj.MyB;

             return newObj;
        }
}

这将不是克隆的孩子,当只有父母是新编辑的工作。在我的code一些班级有较大的层次结构。

This will not work as when Cloning the Child only a parent is new-ed. In my code some classes have large hierarchies.

什么是这样做的推荐方法?克隆的一切,在每个级别,而无需调用基类的似乎是错误的?必须有一些巧妙的解决这个问题,它们是什么?

What is the recommended way of doing this? Cloning everything at each level without calling the base class seems wrong? There must be some neat solutions to this problem, what are they?

我可以感谢大家的答案。这真的很有趣,看到一些办法。我认为这将是很好的,如果有人给了完整性的反映答案的例子。 +1等候!

Can I thank everyone for their answers. It was really interesting to see some of the approaches. I think it would be good if someone gave an example of a reflection answer for completeness. +1 awaiting!

推荐答案

典型的方法是使用拷贝构造函数模式一拉C ++:

The typical approach is to use "copy constructor" pattern a la C++:

 class Base : ICloneable
 { 
     int x;

     protected Base(Base other)
     {
         x = other.x;
     }

     public virtual object Clone()
     {
         return new Base(this);
     }
 }

 class Derived : Base
 { 
     int y;

     protected Derived(Derived other)
          : Base(other)
     {
         y = other.y;
     }

     public override object Clone()
     {
         return new Derived(this);
     }
 }

另一种方法是使用 Object.MemberwiseClone 在执行克隆 - 这将确保结果总是正确的类型,并允许覆盖扩展:

The other approach is to use Object.MemberwiseClone in the implementation of Clone - this will ensure that result is always of the correct type, and will allow overrides to extend:

 class Base : ICloneable
 { 
     List<int> xs;

     public virtual object Clone()
     {
         Base result = this.MemberwiseClone();

         // xs points to same List object here, but we want
         // a new List object with copy of data
         result.xs = new List<int>(xs);

         return result;
     }
 }

 class Derived : Base
 { 
     List<int> ys;

     public override object Clone()
     {
         // Cast is legal, because MemberwiseClone() will use the
         // actual type of the object to instantiate the copy.
         Derived result = (Derived)base.Clone();

         // ys points to same List object here, but we want
         // a new List object with copy of data
         result.ys = new List<int>(ys);

         return result;
     }
 }

这两种方法都需要在层次结构中的所有类遵循的模式。使用哪一个是preference的问题。

Both approaches require that all classes in the hierarchy follow the pattern. Which one to use is a matter of preference.

如果你只是有任何随机类实现 ICloneable 上执行任何保证(除了以下 ICloneable ),有没有办法把它扩大。

If you just have any random class implementing ICloneable with no guarantees on implementation (aside from following the documented semantics of ICloneable), there's no way to extend it.

这篇关于我需要实现C#深拷贝构造函数与继承。是什么模式有可供选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:41