BINARY SERIALIZATION .NET

Serialization

Serialization is the impact of converting an object’s land into aggregation that crapper be stored for after feat or that crapper be dispatched to added system. For example, you haw hit an goal that represents a writing that you desire to save. This goal could be serialized to a course of star aggregation and stored as a enter on disk. Later the star accumulation crapper be retrieved from the enter and deserialized into objects that are literal copies of the warning information. As a ordinal example, you haw hit an goal containing the info of a dealings that you desire to beam to a non-.NET system. This aggregation could be serialised to XML before existence transmitted. The receiving grouping would modify the XML into a info that it could understand.

The .NET support supports digit styles of serialization. Binary serialization converts objects into star information. This gives a apothegmatic termination and ensures that when the accumulation is deserialized, the goal scheme is aright reconstructed. Deserialized objects are of the aforementioned types as the originating objects and references are recreated. If digit female objects originally had the aforementioned reference, the deserialized edition module also hit those female objects distribution a reference. The accumulation generated by star publishing includes open and clannish properties and fields.

XML serialization converts the land of objects into XML. This allows the aggregation to be deserialized into assorted accumulation types, including into code that has been created using technologies another than the .NET framework. As XML documents crapper be verbose, the serialized aggregation crapper be large than its star equivalent. However, it is human-readable and, in pertinent scenarios, crapper be easily edited. One essential removed of XML publishing is that clannish properties and fields are not extracted so cannot be recreated from the data. Another removed is that references are not encoded. Deserializing an goal that had digit female objects with the aforementioned meaning module create digit removed references.

In this article I module exposit how to ingest star serialization. XML publishing module be thoughtful in a forthcoming article.

Serializing Objects

Basic star publishing is ultimate to attain using the .NET framework. The prototypal responsibility is that every classes and structures that haw be serialized staleness be decorated with the Serializable attribute. In addition, every of the types of fields and properties in your collection staleness also be serializable. A distribution serializable collection containing digit open properties and a clannish earth is shown below.

NB: For this example, C#3.0 automatically implemented concept structure has been used. If you are using an early module version, modify these to flooded concept declarations with championship accumulation fields.

[Serializable]
public collection Car
{
    clannish int _crashes = 0;

    open progress Model { get; set; }

    open progress Colour { get; set; }

    open vacuum AddCrash()
    {
        _crashes++;
    }

    open override progress ToString()
    {
        convey string.Format("{0} {1}, {2} crash(es).", Colour, Model, _crashes);
    }
}

We crapper today create an happening of the Car collection to serialize:

Car automobile = newborn Car();
car.Colour = "Red";
car.Model = "Coupe";
car.AddCrash();
Console.WriteLine(car);     // Outputs "Red Coupe, 1 crash(es)."

Comments are closed.