How do i read back Deserialize() ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have this code: First im doing Serialize:
<pre class="lang-cs prettyprint prettyprinted
Code:
<span class="kwd private<span class="pln  <span class="kwd void<span class="pln  <span class="typ Serialize<span class="pun ()<span class="pln 
            <span class="pun {<span class="pln 
                <span class="typ Hashtable<span class="pln  addresses <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ Hashtable<span class="pun ();<span class="pln 
                addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "regular"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueList<span class="pun );<span class="pln 
                addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "R"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueListR<span class="pun );<span class="pln 
                addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "G"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueListG<span class="pun );<span class="pln 
                addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "B"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueListB<span class="pun );<span class="pln 

                <span class="typ FileStream<span class="pln  fs <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ FileStream<span class="pun (<span class="pln f<span class="pun .<span class="pln fileName<span class="pun ,<span class="pln  <span class="typ FileMode<span class="pun .<span class="typ Create<span class="pun );<span class="pln 
                <span class="typ BinaryFormatter<span class="pln  formatter <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ BinaryFormatter<span class="pun ();<span class="pln 
                <span class="kwd try<span class="pln 
                <span class="pun {<span class="pln 
                    formatter<span class="pun .<span class="typ Serialize<span class="pun (<span class="pln fs<span class="pun ,<span class="pln  addresses<span class="pun );<span class="pln 
                <span class="pun }<span class="pln 
                <span class="kwd catch<span class="pln  <span class="pun (<span class="typ SerializationException<span class="pln  e<span class="pun )<span class="pln 
                <span class="pun {<span class="pln 
                    <span class="typ Console<span class="pun .<span class="typ WriteLine<span class="pun (<span class="str "Failed to serialize. Reason: "<span class="pln  <span class="pun +<span class="pln  e<span class="pun .<span class="typ Message<span class="pun );<span class="pln 
                    <span class="kwd throw<span class="pun ;<span class="pln 
                <span class="pun }<span class="pln 
                <span class="kwd finally<span class="pln 
                <span class="pun {<span class="pln 
                    fs<span class="pun .<span class="typ Close<span class="pun ();<span class="pln 
                <span class="pun }<span class="pln 

                <span class="typ Options_DB<span class="pun .<span class="pln set_histogramFileDirectory<span class="pun (<span class="pln f<span class="pun .<span class="pln fileName<span class="pun );<span class="pln 
            <span class="pun }
[/code]
Now i want to do a Deserialize: For example i want to put the List f.histogramValueList into a local variable in Form1 list_of_histograms. list_of_histograms is a type List And f.histogramValueList is type of List
So before i just did:
<pre class="lang-cs prettyprint prettyprinted
Code:
<span class="pln fileName <span class="pun =<span class="pln  <span class="typ Options_DB<span class="pun .<span class="pln get_histogramFileDirectory<span class="pun ();<span class="pln 
            <span class="kwd if<span class="pln  <span class="pun (<span class="typ File<span class="pun .<span class="typ Exists<span class="pun (<span class="pln fileName<span class="pun ))<span class="pln 
            <span class="pun {<span class="pln 
                <span class="typ BinaryReader<span class="pln  binReader <span class="pun =<span class="pln 
                    <span class="kwd new<span class="pln  <span class="typ BinaryReader<span class="pun (<span class="typ File<span class="pun .<span class="typ Open<span class="pun (<span class="pln fileName<span class="pun ,<span class="pln  <span class="typ FileMode<span class="pun .<span class="typ Open<span class="pun ));<span class="pln 
                <span class="kwd try<span class="pln 
                <span class="pun {<span class="pln 
                    <span class="com //byte[] testArray = new byte[3];<span class="pln 
                    <span class="typ int<span class="pln  pos <span class="pun =<span class="pln  <span class="lit 0<span class="pun ;<span class="pln 
                    <span class="typ int<span class="pln  length <span class="pun =<span class="pln  <span class="pun (<span class="typ int<span class="pun )<span class="pln binReader<span class="pun .<span class="typ BaseStream<span class="pun .<span class="typ Length<span class="pun ;<span class="pln 

                    binReader<span class="pun .<span class="typ BaseStream<span class="pun .<span class="typ Seek<span class="pun (<span class="lit 0<span class="pun ,<span class="pln  <span class="typ SeekOrigin<span class="pun .<span class="typ Begin<span class="pun );<span class="pln 

                    <span class="kwd while<span class="pln  <span class="pun (<span class="pln pos <span class="pun <<span class="pln  length<span class="pun )<span class="pln 
                    <span class="pun {<span class="pln 
                        <span class="kwd long<span class="pun []<span class="pln  l <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="kwd long<span class="pun [<span class="lit 256<span class="pun ];<span class="pln 

                        <span class="kwd for<span class="pln  <span class="pun (<span class="typ int<span class="pln  i <span class="pun =<span class="pln  <span class="lit 0<span class="pun ;<span class="pln  i <span class="pun <<span class="pln  <span class="lit 256<span class="pun ;<span class="pln  i<span class="pun ++)<span class="pln 
                        <span class="pun {<span class="pln 
                            <span class="kwd if<span class="pln  <span class="pun (<span class="pln pos <span class="pun <<span class="pln  length<span class="pun )<span class="pln 
                                l<span class="pun [<span class="pln i<span class="pun ]<span class="pln  <span class="pun =<span class="pln  binReader<span class="pun .<span class="typ ReadInt64<span class="pun ();<span class="pln 
                            <span class="kwd else<span class="pln 
                                <span class="kwd break<span class="pun ;<span class="pln 

                            pos <span class="pun +=<span class="pln  <span class="kwd sizeof<span class="pun (<span class="typ Int64<span class="pun );<span class="pln 
                        <span class="pun }<span class="pln 
                        list_of_histograms<span class="pun .<span class="typ Add<span class="pun (<span class="pln l<span class="pun );<span class="pln 
                    <span class="pun }<span class="pln 
                <span class="pun }<span class="pln 

                <span class="kwd catch<span class="pln 
                <span class="pun {<span class="pln 
                <span class="pun }<span class="pln 
                <span class="kwd finally<span class="pln 
                <span class="pun {<span class="pln 
                    binReader<span class="pun .<span class="typ Close<span class="pun ();<span class="pln 
                <span class="pun }
[/code]
But now its not the same now im doing:
<pre class="lang-cs prettyprint prettyprinted
Code:
<span class="kwd private<span class="pln  <span class="kwd void<span class="pln  <span class="typ Deserialize<span class="pun ()<span class="pln 
            <span class="pun {<span class="pln 
                <span class="com // Declare the hashtable reference.<span class="pln 
                <span class="typ Hashtable<span class="pln  addresses <span class="pun =<span class="pln  <span class="kwd null<span class="pun ;<span class="pln 

                <span class="com // Open the file containing the data that you want to deserialize.<span class="pln 
                <span class="typ FileStream<span class="pln  fs <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ FileStream<span class="pun (<span class="pln fileName<span class="pun ,<span class="pln  <span class="typ FileMode<span class="pun .<span class="typ Open<span class="pun );<span class="pln 
                <span class="kwd try<span class="pln 
                <span class="pun {<span class="pln 
                    <span class="typ BinaryFormatter<span class="pln  formatter <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ BinaryFormatter<span class="pun ();<span class="pln 

                    <span class="com // Deserialize the hashtable from the file and  <span class="pln 
                    <span class="com // assign the reference to the local variable.<span class="pln 
                    addresses <span class="pun =<span class="pln  <span class="pun (<span class="typ Hashtable<span class="pun )<span class="pln formatter<span class="pun .<span class="typ Deserialize<span class="pun (<span class="pln fs<span class="pun );<span class="pln 
                <span class="pun }<span class="pln 

                <span class="kwd catch<span class="pln  <span class="pun (<span class="typ SerializationException<span class="pln  e<span class="pun )<span class="pln 
                <span class="pun {<span class="pln 
                    <span class="typ Console<span class="pun .<span class="typ WriteLine<span class="pun (<span class="str "Failed to deserialize. Reason: "<span class="pln  <span class="pun +<span class="pln  e<span class="pun .<span class="typ Message<span class="pun );<span class="pln 
                    <span class="kwd throw<span class="pun ;<span class="pln 
                <span class="pun }<span class="pln 
                <span class="kwd finally<span class="pln 
                <span class="pun {<span class="pln 
                    fs<span class="pun .<span class="typ Close<span class="pun ();<span class="pln 
                <span class="pun }<span class="pln 

                <span class="com // To prove that the table deserialized correctly,  <span class="pln 
                <span class="com // display the key/value pairs. <span class="pln 
                <span class="kwd foreach<span class="pln  <span class="pun (<span class="typ DictionaryEntry<span class="pln  de <span class="kwd in<span class="pln  addresses<span class="pun )<span class="pln 
                <span class="pun {<span class="pln 
                    <span class="typ Console<span class="pun .<span class="typ WriteLine<span class="pun (<span class="str "{0} lives at {1}."<span class="pun ,<span class="pln  de<span class="pun .<span class="typ Key<span class="pun ,<span class="pln  de<span class="pun .<span class="typ Value<span class="pun );<span class="pln 

                <span class="pun }<span class="pln 
            <span class="pun }
[/code]
So how do i end with f.histogramValueList to be in list_of_histograms and also the other Lists in another Lists also the other Lists are

<pre class="lang-cs prettyprint prettyprinted
Code:
<span class="pln addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "regular"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueList<span class="pun );<span class="pln 
                    addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "R"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueListR<span class="pun );<span class="pln 
                    addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "G"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueListG<span class="pun );<span class="pln 
                    addresses<span class="pun .<span class="typ Add<span class="pun (<span class="str "B"<span class="pun ,<span class="pln  f<span class="pun .<span class="pln histogramValueListB<span class="pun );
[/code]
All the Lists f. Are
I need that the numbers in f.histogramValueList will be in list_of_histograms like it was before.
In the end of the process the List: list_of_histograms should contain many Lists inside each of 256 numbers of the f.histogramValueList like i did before.

Before using Serialize and Deserialize when i red the file the dat(binary) file the List: list_of_histograms was for example in index[0] : {long[256]} same for index[1] and so on..
Thats what i need to get in the end too. f.histogramValueList should be in list_of_histograms and then f.histogramValueListR should be in another variable for example: list_of_histogramsR

And same for f.histogramValueListG and f.histogramValueListB

<hr class="sig danieli

View the full article
 
Back
Top