What is serialization? Why do we need it? Why objects need to be
serialized
if they need to be stored in session or viewstate?
Serialization is the process of converting an object to a data stream. The
process consists of two phases serialization and deserialization. You can
compare these phases to dehydration and rehydration of food.
When an object is serialized, or dehydrated, the state information is
extracted. The common elements (think of water), such as methods are not
included in the data stream. When the object is deserialized, or rehydrated,
a new instance of the object is created and the state is restored form the
serialized data.
You have two important types of serialization; binary and XML. The easiest
to explain is XML serialization. When serializing an object to XML the
member names map directly to the XML element names. Consider the following
object instance.
Person
FirstName (string) "Anders"
LastName (string) "Norås"
Age (int) 28
SayHello (void) (method)
When serialized, the Person instance should result in the following XML
document.
<Person xmlns:xsd="
http://www.w3.org/2001/XMLSchema"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns="
http://tempuri.org/">
<FirstName>Anders</FirstName>
<LastName>Norås</LastName>
<Age>28</Age>
</Person>
The SayHello method is not part of the object state and hence it is not
included in the XML document.
ViewState stores all objects as text, therefore an object must be serialized
to a text string before it can be stored in ViewState.
Anders Norås
http://dotnetjunkies.com/weblog/anoras