c# - DeepCopy A SortedDictionary -


i have following :

sorteddictionary<int, sorteddictionary<int, volumeinfoitem>> 

that want deepcopy.

volumeinfoitem following class :

[serializable] public class volumeinfoitem {     public double = 0;     public double down = 0;     public double neutral = 0;     public int dailybars = 0;  } 

i have created following extension method :

public static t deepclone<t>(this t a) {     using (memorystream stream = new memorystream())     {         binaryformatter formatter = new binaryformatter();         formatter.serialize(stream, a);         stream.position = 0;         return (t)formatter.deserialize(stream);     } } 

i can't figure out how deepcopy working ?

your code looks in 1 of answers of question: how do deep copy of object in .net (c# specifically)?

but, since know type of dictionary's contents, can't manually?

// assuming dict original dictionary var copy = new sorteddictionary<int, sorteddictionary<int, volumeinfoitem>>(); foreach(var subdict in dict) {     var subcopy = new sorteddictionary<int, volumeinfoitem>();     foreach(var data in subdict.value)     {         var item = new volumeinfoitem                    {                        = data.value.up,                        down = data.value.down,                        neutral = data.value.neutral,                        dailybars = data.value.dailybars                    };         subcopy.add(data.key, item);     }      copy.add(subdict.key, subcopy); } 

compiled in head, few syntax errors might have slipped by. made bit more compact linq.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -