C# 串行化与反串行化--使用BinaryFormatter进行串行化
来源:互联网 发布:java 泛型 getclass 编辑:程序博客网 时间:2024/06/10 02:38
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。
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- C# 串行化与反串行化--使用SoapFormatter进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
- C# 串行化与反串行化--使用SoapFormatter进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
- C# 串行化与反串行化
- C# 串行化与反串行化
- C# 串行化与反串行化--自定义序列化
- C# 串行化与反串行化--自定义序列化
- C#基础知识回顾--串行化与反串行化
- 深入浅出之C# 串行化与反串行化
- 对象串行化----BinaryFormatter简介
- 对象串行化和反串行化
- PHP 之 串行化与反串行化
- PHP对象的串行化与反串行化
- 订单数量超过forecast数量问题
- MySQL查询获取行号rownum
- the delegate in c shap
- android 按返回退出应用
- 遗传算法入门
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- hdu 1261 (大数除法)
- 泛泰A850工程机与正式机分区表对比
- Dialog七种模式详解
- 手机客户端和服务器通信时如何安全高效的进行身份验证?
- 类型字符转换
- MyEclipse8.5安装aptana插件说明
- C# 串行化与反串行化--使用SoapFormatter进行串行化
- js屏蔽(禁止)键盘和鼠标功能键