The opposite of serialization is deserialization which is a process that converts the outcome of serialization into the original object. NET Framework offers two serialization technologies, binary and XML. Binary Serialization Binary serialization set option binary type system fidelity. Type system fidelity denotes that type information is not lost in the serialization process.
During the serialization process, public and private fields of the object and the name of the class and the containing assembly are converted to a stream of bytes written to a data stream. During deserialization, an exact replica of the object along with type information is reconstructed. XML Serialization This is covered in the XML Serialization chapter. Serialization Concepts Why would you want to use serialization. The ability to convert objects to and from a byte stream can be incredibly useful.
NET saves and restores session state using serialization and de-serialization. A set of object can be easily copied into the clipboard and then pasted into some other application. An object can be cloned using serialization and set aside while the user manipulates the original object. Once object have been serialized, it is quite easy to perform many other functions such as compression, encryption, and so on.
Serialization provides a convenient design pattern for achieving this objective with minimal effort. The CLR manages how objects are laid out in memory and provides an automated serialization mechanism using reflection. When an object is serialized, the name of the class, its containing assembly and all fields are written to storage. When an object is serialized, the CLR also keeps track of all referenced objects already serialized to ensure that the same object is not serialized more than once. The serialization architecture provided with the . Marshal By Value Objects are only valid in the application domain where they were created.
The object is marked with . When an object is marked with , the object will be automatically serialized, transported from one application domain to another and then deserialized to produce an exact copy of the object in the second application domain. Serialization Guidelines When designing new classes, serialization should be one of the issues to consider. Will this class be sent across application domains? Will this class ever be used with remoting? Will this class ever need to be persisted?
Will this class be used as a base class to create derived classes that need to be serialized? The class will never have to cross application domains. The class stored references that are only applicable to the current instance. Basic Serialization The class that performs the actual serialization and deserialization is called a formatter. A formatter is a class type that implements System. When a type is designed, the developer must make an explicit decision as to whether to allow instances of this type of be serialized or not. By default types are not serializable.
If a derived class is marked with , all base classes in the class hierarchy must also be marked with . Stream abstract base class – i. Serialize an object into the storage medium referenced by ‘stream’ object. Deserialize the object and use it. In this aspect, binary serialization differs from XML serialization which serializes only public fields. It is important to note that constructors are not called when objects are deserialized.