对于对象序列化,需要参考下面的代码。在这里,我们使用 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);
以上就是C# 对象序列化的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号