tags:

views:

301

answers:

4

I came across many questions on deep copy but non of them helped me I have a class say

class A
{
     ...
    public List<B> ListB;
    ....
}

where B is again another class which inturn may inherit/contain some other classes Take this scenario

  1. A is a very huge class and contain many reference types
  2. I can not mark B as serializable as i don't have access to source code of B(Though I can Mark A as serializable)

Problem:below methods to perform deep copy does not work because

  1. I can not use Iclonable, memberwise clone technique as class A conatins many reference types
  2. I can not write a copy constructor for A , as the class is huge and keeps growing and contained classes (Like B) can't be deep copied
  3. I can't use serialization technique as i can not mark conatined class(like B, for which no source code avilaable) as serializable

So how can I deep copy the object of Class A?
(I read about "surrogate serialization" technique some where but not clear)

A: 

Can't XML serialization help you?

Andrey
Sounds like his design is so screwed up it would choke. One simple Dictionary<k,v> in the object tree and xml serialization (well, the XmlSerializer) would choke.
Will
+2  A: 

Can't you do this?

[Serializable]
class A
{
     ...
    [NonSerialized]
    public List<B> ListB;
    ....
}

And then refer to http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically for a cloning function

m_oLogin
@m_oLogin: The OP wants it without marking it `Serializable`.
KMan
From what I understand, the author states that he cannot mark the inner class as [Serializable] because he doesn't have access to the source code... He can still mark the inner "ListB" member as [NonSerialized] and then use serialization for deep copying.
m_oLogin
+3  A: 

I stopped using serialization for deep copying anyway, because there is not enough control (not every class needs to be copied the same way). Then I started to implement my own deep copy interfaces and copy every property in the way it should be copied.

Typical ways to copy a referenced type:

  • use copy constructor
  • use factory method (eg. immutable types)
  • use your own "Clone"
  • copy only reference (eg. other Root-Type)
  • create new instance and copy properties (eg. types not written by yourself lacking a copy constructor)

Example:

class A
{
  // copy constructor
  public A(A copy) {}
}

// a referenced class implementing 
class B : IDeepCopy
{
  object Copy() { return new B(); }
}

class C : IDeepCopy
{
  A A;
  B B;
  object Copy()
  {
    C copy = new C();

    // copy property by property in a appropriate way
    copy.A = new A(this.A);
    copy.B = this.B.Copy();
  }
}

You may think that this a huge amount of work. But at the end, it is easy and straight forward, can be tuned where needed and does exactly what you need.

Stefan Steinegger
+1  A: 

your interface IDeepCopy is exactly what ICloneable specifies.

class B : ICloneable
{
     public object Clone() { return new B(); }
}

and with more friendly implementation :

class B : ICloneable
{
     public B Clone() { return new B(); }
     // explicit implementation of ICloneable
     object ICloneable.Clone() { return this.Clone(); }
}
JRC