日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#序列化

發布時間:2025/4/9 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#序列化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.序列化一般有2種(XML和2進制),簡單對象序列化

using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Windows.Forms; using System.Xml.Serialization;namespace Test {//對于XmlSerializer序列化,默認即使不使用特性Serializable,也是可以對對象進行序列化的,則BinaryFormatter不然一定要使用Serializable標記。public partial class Form1 : Form{//XmlSerializer是XML序列化XmlSerializer xs = new XmlSerializer(typeof(Student));//二進制序列化BinaryFormatter b = new BinaryFormatter();Student student = new Student() { Name = "小明", Age = 15 };public Form1(){InitializeComponent();//xml序列化using (Stream stream = new FileStream("d:\\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read)){xs.Serialize(stream, student);}//xml反序列化using (FileStream fs = new FileStream("d:\\Student.xml", FileMode.Open, FileAccess.Read)){Student student = (Student)xs.Deserialize(fs);}//二進制序序列化using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create)){BinaryFormatter b = new BinaryFormatter();//序列化類要加[Serializable]特性 b.Serialize(fileStream, student);}//二進制序反序列化using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read)){BinaryFormatter bf = new BinaryFormatter();student = (Student)bf.Deserialize(fileStream);}}} }[Serializable] public class Student {public string Name { get; set; }public int Age { get; set; } }

?2.復雜對象序列化

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Serialization;namespace Test {public partial class Form1 : Form{//XmlSerializer是XML序列化XmlSerializer xs = new XmlSerializer(typeof(TeacherStudent));//二進制序列化BinaryFormatter b = new BinaryFormatter();TeacherStudent teacherStudent = new TeacherStudent();Teacher teacher = new Teacher() { Name = "王老師", Age = 45 };Student student = new Student() { Name = "小明", Age = 15 };public Form1(){InitializeComponent();//xml序列化using (Stream stream = new FileStream("d:\\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read)){teacherStudent.Teacher = teacher;teacherStudent.Student = student;xs.Serialize(stream, teacherStudent);}//xml反序列化using (FileStream fs = new FileStream("d:\\Student.xml", FileMode.Open, FileAccess.Read)){teacherStudent = null;teacherStudent = (TeacherStudent)xs.Deserialize(fs);}//二進制序序列化using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create)){BinaryFormatter b = new BinaryFormatter();b.Serialize(fileStream, teacher);b.Serialize(fileStream, student);}//二進制序反序列化using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read)){teacher = null;student = null;BinaryFormatter bf = new BinaryFormatter();teacher = (Teacher)bf.Deserialize(fileStream);student = (Student)bf.Deserialize(fileStream);}}} }[Serializable] public class Student {public string Name { get; set; }public int Age { get; set; } }[Serializable] public class Teacher {public string Name { get; set; }public int Age { get; set; } }[Serializable] public class TeacherStudent {public Teacher Teacher { get; set; }public Student Student { get; set; } }

3.?控制序列化/反序列化前后的數據

using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Windows.Forms;namespace Test {public partial class Form1 : Form{//XmlSerializer是XML序列化BinaryFormatter b = new BinaryFormatter();Student student = new Student() { Name = "小明", Age = 15 };public Form1(){InitializeComponent();//二進制序序列化using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create)){BinaryFormatter b = new BinaryFormatter();b.Serialize(fileStream, student);}//二進制序反序列化using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read)){BinaryFormatter bf = new BinaryFormatter();student = (Student)bf.Deserialize(fileStream);}}} }[Serializable] public class Student {public string Name { get; set; }public int Age { get; set; }[OnSerializing()]internal void OnSerializingMethod(StreamingContext context){//格式化器在序列化開始之前調用此方法。Console.WriteLine("OnSerializing格式化器在序列化開始之前調用此方法");}[OnSerialized()]internal void OnSerializedMethod(StreamingContext context){//格式化器在序列化后調用此方法。Console.WriteLine("OnSerialized格式化器在序列化后調用此方法");}[OnDeserializing()]internal void OnDeserializingMethod(StreamingContext context){//格式化器在反序列化開始之前調用此方法。Console.WriteLine("OnDeserializing格式化器在反序列化開始之前調用此方法");}[OnDeserialized()]internal void OnDeserializedMethod(StreamingContext context){//格式化器在序列化開始之前調用此方法。Console.WriteLine("OnDeserialized格式化器在序列化開始之前調用此方法");} }

?

轉載于:https://www.cnblogs.com/lgxlsm/p/5860812.html

總結

以上是生活随笔為你收集整理的C#序列化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。