C# 对象序列化
对于对象序列化,需要参考下面的代码。在这里,我们使用 BinaryFormatter.Serialize(stream,reference)方法来序列化我们的示例对象。
我们在这里设置了一个构造函数 -
public Employee(int id, string name, int salary) { this.id = id; this.name = name; this.salary = salary; }登录后复制
FileStream fStream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); BinaryFormatter bFormat = new BinaryFormatter();登录后复制
Employee emp = new Employee(001, "Jim", 30000); bFormat.Serialize(fStream, emp);登录后复制