Problem: Need to store contents of object to a file? Send an object -> another process? Transmit object across network?
Solution: Will need to convert data -> another format. This is serialization.
Serialization
- System.Runtime.Serialization.
- serialize/deserialize - store/transfer/re-create.
- Serialize an Object - 1) create stream obj to hold serialized output, 2) create BinaryFormatter obj, 3) BinaryFormatter.Serialize().
- Deserialize an Object - 1) create stream object to read the serialized output, 2) create BinaryFormatter obj, 3) create new obj to store deserialized data, 4) BinaryFormatter.Deserialize().
- Serialize/deserialize custom classes - Serializable attribute.
- disable serialization of specific members - [NonSerialized] public decimal total;
- here 'total' member will not be initialized on deserialization. To automatically init. non-serialized member - IDeserializationCallback interface. Impl. IDeserializationCallback.OnDeserialization().
- Version compatiblity tools - 1) 'custom serialization', 2) OptionalField attr - added to newly added members that might cause version compatibility probs. Leaves member's value as null if wasn't serialized, rather than throwing an exception. Initialize optinoal members ~ OnDeserialization() callback mthd.
- Serialization format #1 - BinaryFormatter - most efficient way to serialize objs that will be read only by .NF-based apps.
- Serialization format #2 - SoapFormatter - XML-bsaed formatter - better for transmitting across network or where read by non-.NF-based app.
No comments:
Post a Comment