
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
186
|
Chapter 3: Classes and Structures
chore in and of itself. If a change is made to the fields of the object being cloned, the
deep copy code must also change to reflect this modification. Using serialization, you
rely on the serializer to dynamically find and serialize all fields contained in the
object. If the object is modified, the serializer will still make a deep copy without any
code modifications.
One reason you might want to do a deep copy by hand is that the serialization tech-
nique presented in this recipe works properly only when everything in your object is
serializable. Of course, manual cloning doesn’t always help there either—some
objects are just inherently noncloneable. Suppose you have a network management
application in which an object represents a particular printer on your network.
What’s it supposed to do when you clone it? Fax a purchase order for a new printer?
One problem inherent with deep copying is performing a deep copy on a nested data
structure with circular references. This recipe manages to make it possible to deal
with circular references, although it’s a tricky problem. So, in fact, you don’t need to
avoid circular references if you are using this recipe.
See Also
See the “ICloneable Interface” and “Object.MemberwiseClone Method” topics in the
MSDN ...