如何实现自定义序列化

如何实现自定义序列化

本文介绍了如何实现自定义序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在准备托尼·诺斯鲁普(Tony Northrup)撰写的MCTS 70-536考试.
在本书中,我无法理解如何实现自定义序列化.
请给我一些建议....

Hi,

I''m preparing for an exam of MCTS 70-536 book written by Tony Northrup.
In this book am unable to understand how to impliment custom serialization.
Please give me some suggestions....

[Serializable]
class ShoppingCartItem : ISerializable
{
    public Int32 productId;
    public decimal price;
    public Int32 quantity;
    [NonSerialized]
    public decimal total;

    public ShoppingCartItem(int _productId, decimal _price, int _quantity)
    {
        productId = _productId;
        price = _price;
        quantity = _quantity;
        total = price * quantity;
    }
    protected ShoppingCartItem(SerializationInfo info, StreamingContext context)
    {
        productId = info.GetInt32("Product ID");
        price = info.GetDecimal("Price");
        quantity = info.GetInt32("Quantity");
        total = price * quantity;
    }

    [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);
    }
    public override string ToString()
    {
        return productId + ": " + price + " x " + quantity + " = " + total;
    }
}

推荐答案


这篇关于如何实现自定义序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 00:49