Sunday, June 17, 2007

Binary Serialization in .NET

Persisting objects to other medium for future use is one advantage of the serialization mechanisms built into the .NET Framework. Listed below is are 2 code sections showing serialization and deserialization to disk.

SERIALIZE:


FileStream fStream = new FileStream(ConfigurationManager.AppSettings[1].ToString(), FileMode.Open);
BinaryFormatter bin = new BinaryFormatter();
htObject = bin.Deserialize(fStream) as Hashtable;

DESERIALIZE:


Stream binstream = File.OpenWrite(ConfigurationManager.AppSettings[1].ToString());
BinaryFormatter binFmtr = new BinaryFormatter();
binFmtr.Serialize(binstream, m_hashTable);
binstream.Close();

This can be useful in XNA programming. More C++ for DirectX9 posts will be coming next week.

No comments: