473,809 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deep Copying Techniques?

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
Nov 16 '05 #1
5 3121
Steve - DND wrote:
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
Nov 16 '05 #2
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 - DND" <steve!@!digita lnothing.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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

Nov 16 '05 #3
Steve - DND wrote:
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.
Nov 16 '05 #4
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.

"Bennie Haelen" <bh*****@qwest. net> wrote in message
news:ue******** ******@TK2MSFTN GP11.phx.gbl...
Steve - DND wrote:
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

Nov 16 '05 #5
Hi Steve,

"Steve - DND" wrote:
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 (BinaryFormatte r) the object and than deserialize it?
Thanks,
Steve


I think this could be an idea.

by
Mauro
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4537
by: dan | last post by:
without stirring the pot too much -- could someone please point me to whatever documentation exists on the philosophy, semantics, and practical implications of how Python implements the assignment operator? So far I can't find much useful in the regular documentation. I understand the basic concept that names are bound to objects which exist on the heap, but that still doesn't explain why a = b doesn't _always_ cause a to point to the...
4
3041
by: jjkboswell | last post by:
I have an XSD which I have generated a class from using the xsd.exe tool. My XSD contains complex types within it, so that the generated class has member variables which are of types that are also defined in the XSD. I deserialize XML data into instances of that class, and serialize back to XML. So far so good. However, in my code I have the concept of wanting to create new copies of these deserialized objects and modifying some of...
2
1524
by: RickH | last post by:
Using the ms XMLDOM... What techniques do you guys use to create deep elements in code for appending to the main document as the user "adds" complex objects. Currently I keep 3 files to do it like this: 1) The main xml file that the user is editing 2) The xsd file that validates the user-manipulated document 3) A "model" xml file that looks like the user xml file but only
3
8754
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as suggested by O'Reilly (using the "ICloneable" inhereited interface), at least for the .NET framework. ...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.