C# 串行化与反串行化--使用BinaryFormatter进行串行化

1、使用BinaryFormatter进行串行化

串行化的文件是二进制格式,几乎所有的对象都能顺利串行化,目前还没有发现不能串行化的对象。

public enum SexType
    {
        Male,
        Female
    }
    [Serializable()]
    public class Item
    {
        private int id;
        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Item() { } // 必须有默认构造函数,才能xml序列化
        public Item(int id, string name)
        {
            ID = id;
            Name = name;
        }

        public override string ToString()
        {
            return id.ToString() + "," + name;
        }

        public static Item ToObject(string str)
        {
            Item item = new Item();
            item.id = int.Parse(str.Split(',')[0]);
            item.name = str.Split(',')[1];
            return item;
        }
    }
    [Serializable()]
    public class ItemSub : Item
    {
        private SexType sex;
        public SexType Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        public ItemSub() { } // 必须有默认构造函数,才能xml序列化
        public ItemSub(int id, string name, SexType sex)
            : base(id, name)
        {
            this.sex = sex;
        }
    }

    [Serializable()]
    public class ListBuffer : ISerializable
    {
        private List<string> list = new List<string>();

        public void Add(string str)
        {
            lock (list)
            {
                list.Add(str);
            }
        }

        public void Remove(string str)
        {
            lock (list)
            {
                list.Remove(str);
            }
        }

        public int Count
        {
            get { return list.Count; }
        }
    }
    [Serializable()]
    public class ListBufferSub : ListBuffer
    {
        private List<Item> listItem = new List<Item>();

        public void AddItem(Item item)
        {
            lock (listItem)
            {
                listItem.Add(item);
            }
        }

        public void Remove(Item item)
        {
        }
    }

    [Serializable()]
    public class BinarySerialize
    {
        private int id;
        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private SexType sex;
        public SexType Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        private List<string> listStr;
        public List<string> ListStr
        {
            get { return listStr; }
            set { listStr = value; }
        }

        private List<Item> listItem;
        public List<Item> ListItem
        {
            get { return listItem; }
            set { listItem = value; }
        }

        private ListBuffer buffer = new ListBuffer();
        public ListBuffer Buffer
        {
            get { return buffer; }
            set { buffer = value; }
        }

        private ListBufferSub bufferSub = new ListBufferSub();
        public ListBufferSub BufferSub
        {
            get { return bufferSub; }
            set { bufferSub = value; }
        }

        private List<ListBuffer> listBuffer;
        public List<ListBuffer> ListBuffer
        {
            get { return listBuffer; }
            set { listBuffer = value; }
        }
    }

    public sealed class ConfigurationManagerBinarySerialize
    {
        private static string path = System.Windows.Forms.Application.StartupPath + "\\BinarySerialize.bat";

        public static BinarySerialize Get()
        {
            if (!File.Exists(path))
                return null;

            BinaryFormatter b = new BinaryFormatter();
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                return (BinarySerialize)b.Deserialize(fs);
            }
        }

        public static void Set(BinarySerialize hr)
        {
            BinaryFormatter b = new BinaryFormatter();
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                b.Serialize(fs, hr);
            }
        }
    }

备注:

不管是多么复杂的类,BinaryFormatter总是能够正确的进行序列化和反序列化操作,而且不需要做任何特殊的处理。所以对于大型的类,需要序列化时,优先选择BinaryFormatter。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值