Hi
I believe that this is faulty or extremely misleading. While the example provided works from start to finish, it does NOT work if you try load the value from a previously saved file (FileMode.Open) you only ever get back the default singleton values.
Which kind of defies the whole point of serialization to a file.
Steps to reproduce the error are below.
Tested with VS2008 .Net 3.5
First write a different Singleton values to the datafile.dat
At the top of the Main() add the following line so that you change some value in the singleton
Singleton.GetSingleton().SomeString = "not the orginal";
Run the entire Main and you will see nothing seems wrong (the binary is also correctly written)
Then ReRead the serialized file back into memory after restarting
To do this Edit the following lines in the Main() function
Remove the line of code you added in the previous step (.... SomeStrings = "not the orginal"
Update the file stream line of code in the main to Open not create (only open the file)
FileStream fs = newFileStream("DataFile.dat", FileMode.Open);
AND Remove lines:
formatter.Serialize(fs, a1);
fs.Position = 0;
After you have deserialized add the line
Console
.WriteLine(a2[0].SomeString);
Now rerun the app.
you would expect the a2[0].SomeString to be the "not the orginal" but instead you get "This is a string field".
So you have NOT gotten the first value you save but instead always the defaults, which kind of defies the point of saving (serializing) the object.
Cheers
Choco