11.8 ISerializable

As flexible as the preceding approaches are, they still constrain the type implementer somewhat. Type implementers who need the serialized format to use different names than the in-memory members, or who need the mapping of members to serialized data to be something other than 1:1, or who would like to serialize as one type and deserialize as another (proxy) type, may want to take control of their own serialization via ISerializable and the other interfaces and attributes that make up the serialization architecture.

To indicate to the runtime that it wants to fully control serialization and deserialization, a type implements the ISerializable interface, which looks like this:

 namespace System.Runtime.Serialization {   public interface ISerializable {     void GetObjectData(SerializationInfo si, StreamingContext sc);   } } 

The SerializationInfo parameter is a reference to a property bag [1] that you should store the type member values in. Additionally, SerializationInfo also contains properties that you can use to control the type and assembly the instance should deserialize as. The StreamingContext parameter is a structure that contains, among other things, an enumeration value indicating where the serialized instance is headed (to disk, into the remoting plumbing, etc.).

[1] A common data structure storing a collection of key/value pairs; also known as a hashtable or dictionary.

In addition to implementing ISerializable , a type that is controlling its own serialization needs to provide a deserialization constructor that takes two parameters: a SerializationInfo object reference and a StreamingContext structure. The accessibility of this constructor is not important, and can even be private to prevent other clients from using it to create unofficial instances.

Putting this together with the Teams class from the previous example, an implementation of ISerializable with a deserialization constructor might look like the following:

 [Serializable] public sealed class Team : ISerializable {     public string Name;     public Person[ ] Players;     public void GetObjectData(SerializationInfo si, StreamingContext sc) {         si.AddValue("IChangedTheFieldName", Name);         si.AddValue("Players", Players);     }     private Team(SerializationInfo si, StreamingContext sc)  {        Name = si.GetString("IChangedTheFieldName");        Players = (Person[ ])si.GetValue("Players", typeof(Person[ ]));     }     // Rest of class... } 

This implementation of the deserialization constructor is illustrative , but not robust ”real implementations should consider comparing the version of the deserializing type with the version in the SerializationInfo 's AssemblyName property to detect version mismatches and code accordingly .



C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net