Deep Copying Techniques?

  • Thread starter Thread starter Steve - DND
  • Start date Start date
S

Steve - DND

Is there any way to easily deep copy an entire object? Maybe something with
unsafe code to make a full and completely duplicated copy of an object with
no reference ties to the original? I want everything copied, public,
private, internal, events, etc...

Thanks,
Steve
 
I think the short answer is no.

Any arbitrary object may itself have references to objects that don't
provide deep copies of themselves. A deep copy is really only good for a
specific object that provides such a copy from it's own implementation.

To attempt to deep copy using reflection to discover the object requirements
and then Activator to create instances of objects would potentially be
disastrous. What would happen for example if an object declared a
constructor with an Object parameter which internally it cast to some class
or another. You'd never be able to divine from the constructor signature how
to instantiate the object.

I think you're flogging a dead horse on this one.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Steve said:
Is there any way to easily deep copy an entire object? Maybe something with
unsafe code to make a full and completely duplicated copy of an object with
no reference ties to the original? I want everything copied, public,
private, internal, events, etc...

Thanks,
Steve
If the object supports ICloneable, you can use use the "Clone" method,
which is intended for exactly the purpose that your are describing.

Bennie Haelen
 
Steve said:
Is there any way to easily deep copy an entire object? Maybe
something with unsafe code to make a full and completely duplicated
copy of an object with no reference ties to the original? I want
everything copied, public, private, internal, events, etc...

Thanks,
Steve

Reflection might be the way to go here... there are methods like
GetEvents, GetMethods, etc.
 
ICloneable may or may not provide a deep copy. The object is only obliged to
return what it considers to be a copy and the interface does not specify
deep copying as a requisite. Especially not for all references.

Here's another reason why the OP's post would cause problems.

Imaging providing a deep copy of a control that has a Parent object. That
parent would maintain a reference to it's child controls so no only would
such a copy create a useless and bogus parent form but that form's child
object list wouldn't even refer to the object that was copied in the first
place.

The whole concept is nasty and fraught with problems.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hi Steve,

Steve - DND said:
Is there any way to easily deep copy an entire object? Maybe
something with unsafe code to make a full and completely duplicated
copy of an object with no reference ties to the original? I want
everything copied, public, private, internal, events, etc...

An italian MVP answer a similar question with:

why not serializing (BinaryFormatter) the object and than deserialize it?
Thanks,
Steve

I think this could be an idea.

by
Mauro
 
Back
Top