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);
廣告