UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework
UPGRADE: MCAD Skills to MCPD Windows Developer by Using the Microsoft .NET Framework
UPGRADE: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 1
Serialize or deserialize an object or an object graph by using runtime serialization techniques. (Refer System.Runtime.Serialization namespace)
- Serialization interfaces
- Serilization attributes
- SerializationEntry structure and SerializationInfo class
- ObjectManager class
- Formatter class, FormatterConverter class, and FormatterServices class
- StreamingContext structure
Here's an old article by Jeffery Richter that explains most of the stuff about serialization pertaining to this test. It's 1.1, but you'll find that there isn't a lot that has changed.
Serialization Interfaces (System.Runtime.Serialization)
IDeserializationCallback – Provides a method that is called when deserialization is complete – useful for setting internal state after a deserialization.
IFormatter – Exposes methods for serializing/deserializing an object. Used for controlling the format output of the serialization. BinaryFormatter, SoapFormatter are two examples of objects that implement this interface. Note that IFormatter and IFormattable are two different interfaces that do two different things.
IFormatterConverter – Converts objects to different types. Appears functionally identical to IConvertible. Not sure what the difference is aside from IFormatterConverter is called during serialization.
IObjectReference – Used for objects that are “reference” objects – Singletons for example. You wouldn’t want to deserialize a new instance of a singleton. GetRealObject is called in the Fixup stage and should return a reference to the object.
ISerializable – Tells the framework that the developer has provided their own serialization implementation.
ISerializationSurrogate – Allows one object to serialize another.
http://www.codeproject.com/dotnet/Surrogate_Serialization.asp
ISurrogateSelector – Assists the serializer in deciding which Surrogate to use for a particular type. Used in ISerializationSurrogate SetObjectData method. It’s not clear why I would ever need to implement this interface. SurrogateSelector seems to do a pretty good job.
Serialization Attributes
These four are new to 2.0:
OnDeserializingAttribute – decorates a method that is called before object is actually deserialized.
OnDeserializedAttribute – called after class is deserialized. Seems functionally identical to IDeserializationCallback to me.
OnSerializingAttribute – called before an object is serialized.
OnSerializedAttribute – called after an object is serialized.
**Methods decorated with these 4 attributes are expected to have one parameter that is a StreamingContext object.
OptionalFieldAttribute – marks a field as optional, as far as serialization is concerned. This prevents the serializer from freaking if it is not in the stream. New in 2.0
System.Serializable – marks a object as able to be serialized
System.NonSerialized – tells the serializer to ignore the field when serializing
SerializationInfo class
Stores information needed to serialize/deserialize an object. Mostly a collection of serializationEntry structures. Method is AddValue, not Add.
SerializationEntry structure
Contains the Name, Type, and a reference to an object that should be serialized. Used when enumerating through SerializationInfo object.
ObjectManager class
Keeps track of objects as they are deserialized to prevent reserialization (creating the same object twice in memory).
Formatter class
Provides base functions for serialization formatters. Abstract class. BinaryFormatter and SoapFormatter inherit from Formatter.
FormatterConverter class
Base implementation of IFormatterConverter. Not clear where I would use it.
FormatterServices class
Helper object for serialization. GetObjectData, GetSerializableMembers and PopulateObjectMembers are interesting methods that I didn't know existed.
StreamingContext
Describes the source and destination of a given serialized stream, and provides an additional caller-defined context. Can figure out if the object is being serialized CrossProcess, CrossMachine…
Next up - XML Serialization