473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Hashtable cloning :: deep solution using serialization

I realized today that the Hashtable.Clone only produces a shallow
copy... that makes me go mad that M$ doesn't even provide a deep copy
ctor for the Hashtable class !

mighty tech ducks might reply "oh but what if the types in hastable
don't have copy ctors defined? that could lead to dangerous
situations".

well, but was it a significant effort to provide a copy ctor / deep
clone method to this class for objects that provide the IClonable
interface? WTF with M$ that made 'em say in the MSDN that Hashtable
implements IClonable and _don't_ provide a deep copy ctor???

i am wondering... but meanwhile i thought of a trick to do smthg like a
de"ep clone using serialization (=> for objects that implements it) :

1) serialize your hashtable
2) deserialize it into a new one
3) garbage the serialization

to serialiaze, you might want to use this code

<code>
using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
/// <summary>
/// Deep copy of Serializable items
/// </summary>
public class ObjectUtils
{
static BinaryFormatter binForm=new BinaryFormatter();

public static object CloneBySerialization(object source)
{
MemoryStream ms=new MemoryStream();
binForm.Serialize(ms,source);
return binForm.Deserialize(new MemoryStream(ms.ToArray()));
}

}
}
</code>

but is there another way, perhaphs more elegant???

(err i dont' want to implement a Hashtable deep copy :: it's M$ work:)

Nov 17 '05 #1
1 2310
oDDskOOL,

Nope, that's pretty much the most elegant way.

It should be noted that MS doesn't give guidance on the implementation
of IClonable. Doing so would lock them into a pattern, and given the
widespread use of this interface, it is impossible for them to determine
what the best approach would be.

Granted, it might help to have a IDeepClonable and IShallowClonable
interface, but then again, there would be bickering over the exact meaning
of those as well.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"oDDskOOL" <od******@gmail.com> wrote in message
news:11********************@g47g2000cwa.googlegrou ps.com...
I realized today that the Hashtable.Clone only produces a shallow
copy... that makes me go mad that M$ doesn't even provide a deep copy
ctor for the Hashtable class !

mighty tech ducks might reply "oh but what if the types in hastable
don't have copy ctors defined? that could lead to dangerous
situations".

well, but was it a significant effort to provide a copy ctor / deep
clone method to this class for objects that provide the IClonable
interface? WTF with M$ that made 'em say in the MSDN that Hashtable
implements IClonable and _don't_ provide a deep copy ctor???

i am wondering... but meanwhile i thought of a trick to do smthg like a
de"ep clone using serialization (=> for objects that implements it) :

1) serialize your hashtable
2) deserialize it into a new one
3) garbage the serialization

to serialiaze, you might want to use this code

<code>
using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
/// <summary>
/// Deep copy of Serializable items
/// </summary>
public class ObjectUtils
{
static BinaryFormatter binForm=new BinaryFormatter();

public static object CloneBySerialization(object source)
{
MemoryStream ms=new MemoryStream();
binForm.Serialize(ms,source);
return binForm.Deserialize(new MemoryStream(ms.ToArray()));
}

}
}
</code>

but is there another way, perhaphs more elegant???

(err i dont' want to implement a Hashtable deep copy :: it's M$ work:)

Nov 17 '05 #2

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

Similar topics

3
by: A. Burch | last post by:
I'm wanting to use a Hashtable. I have 2 (keys, objectvalues) pairs of data to store. I'm using a static declartion for the Hashtable the instance of the class that is the object. If I try and...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
4
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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,...

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.