We know that we can add the serializable attribute to serialize and deserialize objects. It is usually used to store and transmit objects. For example
[Serializable]
class ShoppingCartItem
{
public int productId;
public decimal price;
public int quantity;
public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
}
But sometimes, we do not need to serialize all the members (it is a waste of storage space and increases the transmission pressure), so we can use
[Nonserialized] To identify attributes and methods. For example
[Serializable]
class ShoppingCartItem
{
public int productId;
public decimal price;
public int quantity;
[NonSerialized]
public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
}
If so, total will not be serialized, but will not be initialized during the deserialization process. But what if we want to get the result of total in the deserialization object? Then we need
The ideserializationcallback interface and implements the ideserializationcallback. ondeserialization method. For example
class ShoppingCartItem : IDeserializationCallback {
public int productId;
public decimal price;
public int quantity;
[NonSerialized] public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
void IDeserializationCallback.OnDeserialization(Object sender)
{
// After deserialization, calculate the total
total = price * quantity;
}
}
We know that we can add the serializable attribute to serialize and deserialize objects. It is usually used to store and transmit objects. For example
[Serializable]
class ShoppingCartItem
{
public int productId;
public decimal price;
public int quantity;
public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
}
But sometimes, we do not need to serialize all the members (it is a waste of storage space and increases the transmission pressure), so we can use
[Nonserialized] To identify attributes and methods. For example
[Serializable]
class ShoppingCartItem
{
public int productId;
public decimal price;
public int quantity;
[NonSerialized]
public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
}
If so, total will not be serialized, but will not be initialized during the deserialization process. But what if we want to get the result of total in the deserialization object? Then we need
The ideserializationcallback interface and implements the ideserializationcallback. ondeserialization method. For example
class ShoppingCartItem : IDeserializationCallback {
public int productId;
public decimal price;
public int quantity;
[NonSerialized] public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
void IDeserializationCallback.OnDeserialization(Object sender)
{
// After deserialization, calculate the total
total = price * quantity;
}
}