static void LookIntoSerialization() { //1 MemberInfo[] members = FormatterServices.GetSerializableMembers(typeof(SClass)); //2 SClass sc = new SClass(1, 1); object[] values = FormatterServices.GetObjectData(sc, members); //3 将对象的程序集表示序列化 //4 将字段值序列化 }
//使用attribute控制格式化流程 static void ControlSerializationByAttribute() { BinaryFormatter bf = new BinaryFormatter(); SClass sc = new SClass(1, 1); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, sc); sc = null; ms.Position = 0; sc = (SClass)bf.Deserialize(ms); }