2、ield)需要注意, 如果类中使用了其它的类属性,如果希望它们能序列化,那么其它的类也必须是可序列化的, 如下面类DethDisease namespace SerializationDemo{ [Serializable] public class Person { [NonSerialized] private int _age; public int Age { get { return _age;
3、 } set { _age = value; } } public DethDisease Disease { get; set; } public string Medicine { get; set; } public Person() {
4、Age = 0; } public bool Live() { Age++; if (Disease != null && !Disease.CanLive(Age, Medicine)) return false; return true; } }}二进制序列化的代码, 主要是这个BinaryFormatter.1234IFormatter formatter = new BinaryFormatter(); St
5、ream stream = new FileStream("Person", FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, newPerson);stream.Close();反序列化1234IFormatter formatter = new BinaryFormatter();Stream stream = new FileStream("Person", FileMode.Open, FileAccess.Read, FileShare.Read);ne