.Net has provided a way to serialize object as you wish, you can have all control when serializing and deserializing any object. You even decided format of storing serialized object. All custom serialization can be done by implementing ISerializable interface and applying Serializable attribute to class.
[Serializable]
class ShoppingCartItem : ISerializable
{
public Int32 productId;
public decimal price;
public Int32 quantity;
[NonSerialized]
public decimal total;
// Constructor used to initialized member of my ShoppingCartItem object.
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
// Constructor will be called on deserialization
protected ShoppingCartItem(SerializationInfo info,
StreamingContext context)
{
productId = info.GetInt32("Product ID");
price = info.GetDecimal("Price");
quantity = info.GetInt32("Quantity");
total = price * quantity;
}
// The following method is called during serialization
[SecurityPermissionAttribute(SecurityAction.Demand,
SerializationFormatter=true)]
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("Product ID", productId);
info.AddValue("Price", price);
info.AddValue("Quantity", quantity);
}
}
Above class shows how to implement custom serialization, we have implemented a constructor, that is called on deserialization. That will assign value to our object member.
We have also added a method, GetObjectData which is used to store value at the time of Serialization.
If we notice on constructor and methods, it uses SerializationInfo object that is used to convert value to different datatype combatable to our member’s type.
BinaryFormatter provides a way to handle serialized and deserialized events. There are four types of events
Serializing: This event is raised just before serialization takes place. Apply the
OnSerializing attribute to the method that should run in response to this event.
Serialized: This event is raised just after serialization takes place. Apply the OnSerialized attribute to the method that should run in response to this event.
Deserializing: This event is raised just before deserialization takes place. Apply the OnDeserializing attribute to the method that should run in response to this event.
Deserialized: This event is raised just after deserialization takes place and after IDeserializationCallback.OnDeserialization has been called. Apply the OnDeserialized attribute to the method that should run in response to this event.
Respond to one of the above events methods should follow below criteria
àStreamingContext object passed to method as parameter
àReturn Void
àAppropriate attribute should be specified on the method.
[OnSerializing]
void CalculateTotal(StreamingContext sc)
{
total = price * quantity;
}
[OnDeserialized]
void CheckTotal(StreamingContext sc)
{
if (total == 0) { CalculateTotal(sc); }
}
We can add above methods to our class to handle serialized events.
With the help of StrreamingContext, application can decide how to serialize or deserailzed object, we get idea of platform, weather object is serialized with same process or else, from where object is coming, object location etc.
To create custom formatter we should implement IFormatter interface, both of the BinaryFormatter and SoapFormatter have implementation of IFormatter. IFormatter provides many method to implement serialization and deserialization as per own custom way.
No comments:
Post a Comment