Deep copy Hashtable
-
Hello, Any one who could give me an advice on how to deep copy a Hashtable in as short time as possible? My first implementation is: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { dst.Clear(); Array copy = Array.CreateInstance(typeof(DictionaryEntry), src.Count); src.CopyTo(copy,0); foreach (DictionaryEntry Entry in copy) { dst.Add(Entry.Key, Entry.Value); } } Thanks Rickard
-
Hello, Any one who could give me an advice on how to deep copy a Hashtable in as short time as possible? My first implementation is: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { dst.Clear(); Array copy = Array.CreateInstance(typeof(DictionaryEntry), src.Count); src.CopyTo(copy,0); foreach (DictionaryEntry Entry in copy) { dst.Add(Entry.Key, Entry.Value); } } Thanks Rickard
First of all, you don't need to
ref
aHashtable
, it's already a reference Type! Second, your method above isn't a deep copy either because it doesn't actually copy the objects stored in the table. An easy way would be to serialize it to aMemoryStream
and deserialize to get the new object. Without an instance manager like .NET Remoting uses, the deserialized objects shouldn't be hooked-up to the ones that they were before. The only stipulation is that the keys and values are serializable, which means they must be attributed withSerializableAttribute
, even if they implementISerializable
. If you wanted to use your code above, make sure that you check each key/value for an impementation ofICloneable
and callClone
when possible.Microsoft MVP, Visual C# My Articles
-
First of all, you don't need to
ref
aHashtable
, it's already a reference Type! Second, your method above isn't a deep copy either because it doesn't actually copy the objects stored in the table. An easy way would be to serialize it to aMemoryStream
and deserialize to get the new object. Without an instance manager like .NET Remoting uses, the deserialized objects shouldn't be hooked-up to the ones that they were before. The only stipulation is that the keys and values are serializable, which means they must be attributed withSerializableAttribute
, even if they implementISerializable
. If you wanted to use your code above, make sure that you check each key/value for an impementation ofICloneable
and callClone
when possible.Microsoft MVP, Visual C# My Articles
Thanks! My new implementation: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { MemoryStream ms= new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); //serialize source to a stream bf.Serialize(ms,src); //deserialize it to the copy ms.Seek(0,0); dst = (Hashtable)bf.Deserialize(ms); } A comment: I had to ref the destination Hashtable otherwise the result of the function was an empty Hashtable. Rickard
-
Thanks! My new implementation: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { MemoryStream ms= new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); //serialize source to a stream bf.Serialize(ms,src); //deserialize it to the copy ms.Seek(0,0); dst = (Hashtable)bf.Deserialize(ms); } A comment: I had to ref the destination Hashtable otherwise the result of the function was an empty Hashtable. Rickard
You don't need the
ref
on the inputHashtable
, though, which is what I was trying to get at. And actually, since the deserialization process creates a newHashtable
, you'd be better off usingout
for the second parameter, that way you don't have to initialize an extraneous instance of aHashtable
that's going to be a new instance after deserialization anyway. So your method signature would look like this:protected void CopyHash(Hashtable src, out Hashtable dst);
Microsoft MVP, Visual C# My Articles