一.可将多个对象序列化到一个流中
List<int> intList = new List<int>(); List<string> strList = new List<string>(); MemoryStream ms = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, intList); formatter.Serialize(ms, strList); ms.Position = 0; var list1 = (List<int>)formatter.Deserialize(ms); var list2 = (List<string>)formatter.Deserialize(ms);
注意点:Formatter类型必须相同,序列化和反序列化顺序要相同
这样就不用把多个对象序列化到多个流当中了.
二.控制序列化和反序列化
[Serializable] public class Person { public string FirstName { get; set; } public string LastName { get; set; } //FirstName+LastName [NonSerialized] private string _name;
Name不需要序列化,因为是两个属性的组合,但Name需要一个初始化的入口点,如下
使用OnDeserialized标记
[Serializable] public class Person { public string FirstName { get; set; } public string LastName { get; set; } //FirstName+LastName [NonSerialized] private string _name; [OnDeserialized] private void OnDeserialized(StreamingContext context) { this._name = FirstName + LastName; } }
全部的标记,ing在前,ed在后
[OnDeserialized] private void OnDeserialized(StreamingContext context) { } [OnDeserializing] private void OnDeserializing(StreamingContext context) { } [OnSerialized] private void OnSerialized(StreamingContext context) { } [OnSerializing] private void OnSerializing(StreamingContext context) { }
三.序列化辅助类
来自MSDN的例子
[Serializable] class Book { public string Title; public string Author; // Constructor for setting new values. public Book(string newTitle, string newAuthor) { Title = newTitle; Author = newAuthor; } }
1.GetSerializableMembers获取可序列化成员
2.GetObjectData以数组形式返回对象数据
3.GetSafeUninitializedObject创建对象新实例(注意:不会执行构造函数)
4.PopulateObjectMembers给对象填充数据
static void Run() { // Create an instance of a Book class // with a title and author. Book Book1 = new Book("Book Title 1", "Masato Kawai"); // Store data about the serializable members in a // MemberInfo array. The MemberInfo type holds // only type data, not instance data. MemberInfo[] members = FormatterServices.GetSerializableMembers (typeof(Book)); // Copy the data from the first book into an // array of objects. object[] data = FormatterServices.GetObjectData(Book1, members); // Create an uninitialized instance of the Book class. Book Book1Copy = (Book)FormatterServices.GetSafeUninitializedObject (typeof(Book)); // Call the PopuluateObjectMembers to copy the // data into the new Book instance. FormatterServices.PopulateObjectMembers (Book1Copy, members, data); // Print the data from the copy. Console.WriteLine("Title: {0}", Book1Copy.Title); Console.WriteLine("Author: {0}", Book1Copy.Author); }
四.自定义序列化行为
如果第二点讲到的控制还不能让你满意的话,可以实现一个ISerializable接口.需要做两件事情.
1.实现GetObjectData方法,添加序列化数据
2.一个反序列化时的构造函数,初始化数据
[Serializable] public class Insect : ISerializable { private string name; private int id; public Insect(string name, int id) { this.name = name; this.id = id; } public override string ToString() { return String.Format("{0}:{1}", name, id); } public Insect() { } public virtual void GetObjectData(SerializationInfo s, StreamingContext c) { s.AddValue("CommonName", name); s.AddValue("ID#", id); } private Insect(SerializationInfo s, StreamingContext c) { name = s.GetString("CommonName"); id = s.GetInt32("ID#")+1; } }
例子转自http://www.cnblogs.com/wdxinren/archive/2004/10/23/55826.html
…