What is the best way to deep clone an interconnected set of objects? Example:
class A {
B theB; // optional
// ...
}
class B {
A theA; // optional
// ...
}
class Container {
A[] a;
B[] b;
}
The obvious thing to do is walk the objects and deep clone everything as I come to it. This creates a problem however -- if I clone an A
that contains a B
, and that B
is also in the Container
, that B
will be cloned twice after I clone the Container
.
The next logical step is to create a Dictionary
and look up every object before I clone it. This seems like it could be a slow and ungraceful solution, however.
Any thoughts?