Serialization
Q) What is serialization?
A) Serialization as implemented in the System.Runtime.Serialization is the process of Serializing and Deserializing objects so that they can stored or transferred and then later recreated.
Q) What is Serializing?
A) Seraializing is a process of converting object into a linear sequence of bytes that can be stored on disk or transferred over network.
Q) What is Deserializing?
A) Deserializing is a process of converting previously serialized sequence of bytes into an object.
E.g. Serializing
FileStream fs = new FileStream(@"c:\test_env\serialized.dat", FileMode.Create, FileAccess.Write);
//binary formatter to perform serialization
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, "This data string will be written to the file as binary data");
fs.Close();
E.g. Deserializing
FileStream fr = new FileStream(@"c:\test_env\serialized.dat", FileMode.Open);
string strIn = string.Empty;
strIn = bf.Deserialize(fr) as string;
Q) How to create a class that can be serialized?
A) If you are satisfied with the default handling of serialization then [Serializable] Attribute to the class is enough, when a class is serialized, runtime serializes all the members including private members.
refer the below example for using various attributes with serialization.
Runtime calls GetObjectData during serialization and serialization constructor during deserialization
[Serializable] //Attribute for serializing and deserializing objects of custom class
class Test:IDeserializationCallback //implement OnDeserialization which will be called once serialization is complete
{
public int i;
public int j;
//total will not be serialized and deserialized, i.e. disable serialization of specific members only.
[NonSerialized] public int total;
//if changes are made to this class, but there are serialized object of
//the instance before this field was added, OptionalField attribute would
//not affect serialization, but during deserialization if the member was not serialized
//it will leave the members value as null rather than throwing exception
[OptionalField] public int offset;
void IDeserializationCallback.OnDeserialization(object sender)
{
total = i + j;
}
}
Two methods for formatting serialized data both of which implement IRemotingFormatter interface.
For BinaryFormatter : Only for .Net Framework
using System.Runtime.Serialization.Formatters.Binary;
For SoapFormatter add reference to "System.Runtime.Serialization.Formatters.Soap" Serialize objects that will be transmitted across network.
using System.Runtime.Serialization.Formatters.Soap;
Consumes 3 to 4 times more space.
XML Serialization:
Can serialize only public data, cannot serialize private data
Cannot serialize graphs, only objects.
E.g.
TestXMLS tm = new TestXMLS(10, 20);
XmlSerializer xm = new XmlSerializer(typeof(TestXMLS));
FileStream fs = File.Create(@"c:\test_env\test.xml");
xm.Serialize(fs, tm);
Wednesday, October 31, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment