Agan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Serialization

1.       What is Serialization?

A mechanism to store the state of an object in order to perform operations on the serialized state,

Deserialization is the reverse process of serialization in which the object is reconstructed back from the serialized state

During the process of Serialization, the objects are marshaled by value.

During Deserialization process, the formatter reads the stream and constructs the object.

2.       Serialization at a high level

3.       Types of Serialization

Shallow Serialization

The XML serialization technique can convert only the public properties and fields into XML format.

Deep SerializationThis inability is overcome by Binary and SOAP serialization techniques in which the state of the entire object is serialized into a stream of bytes. If the object contains references, even those are serialized.

4.       Formatter

Serialization and Deserialization are performed by an object that implements the IFormatter interface.

The .NET framework provides two implementations of this interface

_ BinaryFormatter

_ SoapFormatter

The IFormatter interface provides two methods, Serialize and Deserialize.

The Serialize method takes the object graph and writes its state including the type identity to the stream.

The Deserialize method reads the stream and reconstructs the object graph from the stream.

In addition to the name-value pairs for each of the object fields, formatters also intelligently write the name of the type/class and the assembly in which it is defined, to the stream.

By default, both BinaryFormatter and SoapFormatter write out the full name of the assembly including its filename, version, culture and public key token.

5.       Stream

Stream is an abstract class located in the System.IO namespace.

Stream essentially defines a pattern for storing and retrieving data.

.NET framework provides the following stream classes,

_ to store the data in memory (System.IO.MemoryStream)

_ to store the data in a file (System.IO.FileStream)

_ to send data across the network (System.Net.Sockets.NetworkStream)

6.       Core Namespaces for Serialization

• The .NET framework provides built-in mechanism to serialize and deserialize objects.

• This functionality can be found in the following namespaces,

_ System.Runtime.Serialization

_ System.Xml.Serialization

Note: If BinaryFormatter is used for serializing, then deserialization can be performed only using BinaryFormatter. SoapFormatter cannot be used to deserialize an object serialized using BinaryFormatter.The same applies for SoapFormatter.

7.       1. Serialization using formatters

//Namespace

System

System.IO (for streams)

System.Runtime.Serialization

System.Runtime.Serialization.Formatters

System.Runtime.Serialization.Formatters.Binary

//Code

// Create a stream; Can be FileStream etc.

MemoryStream strm = new MemoryStream();

// Create a serialization formatter; can be any formatter

BinaryFormatter fmtr = new BinaryFormatter();

// Serial object graph into the stream

fmtr.Serialize(strm, objectGraph);

De-serialization

// Create the serialization formatter

BinaryFormatter defmtr = new BinaryFormatter();

// Get the object graph back

Object obj = defmtr.Deserialize(strm);

7.       2. Serialization using XmlSerializer

//Namespace

System

System.IO (for streams)

System.Runtime.Serialization

System.Xml.Serialization

//Code

// Create a stream; could have been FileStream etc.

MemoryStream strm = new MemoryStream();

// Create an instance of XmlSerializer

XmlSerializer xs = new XmlSerializer(typeof(class));

// Serial object graph into the stream

xs.Serialize(strm, objectGraph);

De-serialization using XmlSerializer

// Create the XmlSerializer instance

XmlSerializer xs = new XmlSerializer(typeof(class));

// Get the object graph back

Object obj = xs .Deserialize(strm);

8.       How the framework locates classes

• Serialization adds

v      Typename

v      Assembly identity (full name)

• Deserialization

_ Recovers the Assembly Identity

_ Loads it, if not already loaded; uses Assembly.Load method.

(Looks in GAC first and then in the local directory)

_ If class is found in assembly, deserialization proceeds

_ Else SerializationException is thrown

9.             Points to remember

Mark the class with Serializable Attribute

If base class is not Serializable, derived class cannot be serialized (by design).

Multiple object graphs can be serialized into the same stream.

Deserialization is FIFO order.

XmlSerialization is shallow.

Serialization performed using BinaryFormatter or SoapFormatter is deep.

When a class is marked with Serializable attribute, it will always be marshalled by value

10.         Selective Serialization

Selective serialization using NonSerializable attribute

• Caveats with NonSeriazable attribute

• Deserialization Callback for initializing fields that were prevented from being serialized using NonSerializable attribute

A class may contain fields that should not be serialized . We can prevent member variables from being serialized by NonSerialized attribute.

• After deserialization, fields that were marked with NonSerialized Attribute will be set to default values.

• Alternatively, Deserialization Callback can be used to initialize those fields automatically.

• NonSerialized Attribute works only with Formatters.

阅读全文
类别:.net summary 查看评论
posted on 2008-11-14 17:59  Alan Gan  阅读(164)  评论(0编辑  收藏  举报