首頁  >  文章  >  後端開發  >  C# 物件序列化

C# 物件序列化

PHPz
PHPz轉載
2023-08-28 15:29:08639瀏覽

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類別的一個物件 -

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

以上是C# 物件序列化的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除
上一篇:C# 中的計數器下一篇:C# 中的計數器