C# 对象序列化

csharpprogrammingserver side programming更新于 2025/9/20 9:27:00

对于对象序列化,您需要参考以下代码。在这里,我们使用 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 类的对象 −

Employee emp = new Employee(001, "Jim", 30000);
bFormat.Serialize(fStream, emp);

相关文章