C# 對象耐久化。本站提示廣大學習愛好者:(C# 對象耐久化)文章只能為提供參考,不一定能成為您想要的結果。以下是C# 對象耐久化正文
對象耐久化是指將內存中的對象保管到可永世保管的存儲設備中(如磁盤)的一種技術。
本文引見的是除數據庫之外的幾種對象耐久化方式。
詳細如下:
觸及知識點:
如下圖所示【次要功用是將用戶輸出的信息保管成各種格式,並從各個文檔中停止讀取出來】:

保管和讀取文本文檔代碼如下:

1 /// <summary>
2 /// 保管成文本
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void btnSaveTxt_Click(object sender, EventArgs e)
7 {
8 Dictionary<string, string> dicInfos = GetDictionaryInfos();
9 string filePath = "objPerson.txt";
10 //采用using關鍵字,會自動釋放
11 using (FileStream fs = new FileStream(filePath, FileMode.Create))
12 {
13 using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
14 {
15 foreach (var keyPair in dicInfos)
16 {
17 sw.WriteLine(string.Format("{0}={1}", keyPair.Key, keyPair.Value));
18 }
19 }
20 }
21 }
22
23 /// <summary>
24 /// 從文本中讀取
25 /// </summary>
26 /// <param name="sender"></param>
27 /// <param name="e"></param>
28 private void btnReadTxt_Click(object sender, EventArgs e)
29 {
30 string filePath = "objPerson.txt";
31 Dictionary<string, string> dic = new Dictionary<string, string>();
32 //采用using關鍵字,會自動釋放
33 using (FileStream fs = new FileStream(filePath, FileMode.Open))
34 {
35 using (StreamReader sw = new StreamReader(fs, Encoding.Default))
36 {
37 while (!sw.EndOfStream) {
38 string lineInfo = sw.ReadLine();
39 dic.Add(lineInfo.Split('=')[0], lineInfo.Split('=')[1]);
40 }
41 }
42 }
43 this.txtName.Text = dic["Name"];
44 this.dtBirthday.Text = dic["Birthday"];
45 if (dic["Gender"] == this.rbBoy.Text)
46 {
47 this.rbBoy.Checked = true;
48 }
49 else {
50 this.rbGirl.Checked = true;
51 }
52 string[] loves = dic["Love"].Split('|');
53 foreach (var love in loves)
54 {
55 foreach (var item in this.lsvLove.Items)
56 {
57 ListViewItem li = item as ListViewItem;
58 if (li.Text == love)
59 {
60 li.Checked = true;
61 }
62 }
63 }
64 }
View Code
保管和讀取Xml文檔代碼如下:

1 /// <summary>
2 /// 保管成Xml
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void btnSaveXml_Click(object sender, EventArgs e)
7 {
8 Person p = GetPersonInfos();
9 string filePath = "objPerson.xml";
10 using (FileStream fs = new FileStream(filePath, FileMode.Create))
11 {
12 XmlSerializer serializer = new XmlSerializer(typeof(Person));
13 serializer.Serialize(fs, p);
14 }
15 }
16
17 /// <summary>
18 /// 從Xml中讀取
19 /// </summary>
20 /// <param name="sender"></param>
21 /// <param name="e"></param>
22 private void btnReadXml_Click(object sender, EventArgs e)
23 {
24 string filePath = "objPerson.xml";
25 Person p;
26 using (FileStream fs = new FileStream(filePath, FileMode.Open))
27 {
28 XmlSerializer serializer = new XmlSerializer(typeof(Person));
29 object obj= serializer.Deserialize(fs);
30 p = obj as Person;
31 }
32 SetPersonInfos(p);
33 }
View Code
保管和讀取Json文檔如下:

1 /// <summary>
2 /// 保管成Json
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void btnSaveJson_Click(object sender, EventArgs e)
7 {
8 Person p = GetPersonInfos();
9 JavaScriptSerializer jserializer = new JavaScriptSerializer();
10 string strJson = jserializer.Serialize(p);
11 string strRegex = @"\\/Date\((\d+)\)\\/";
12 MatchEvaluator evaluator = new MatchEvaluator(ConvertJsonDateToDateString);
13
14 //對時間停止處置,需求援用System.Text.RegularExpressions;命名空間
15 Regex reg = new Regex(strRegex);
16 strJson = reg.Replace(strJson, evaluator);
17
18 string filePath = "objPerson.json";
19 using (FileStream fs = new FileStream(filePath, FileMode.Create))
20 {
21 using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
22 {
23 sw.Write(strJson);
24 }
25 }
26 }
27
28 /// <summary>
29 /// 從Json中讀取
30 /// </summary>
31 /// <param name="sender"></param>
32 /// <param name="e"></param>
33 private void btnReadJson_Click(object sender, EventArgs e)
34 {
35 JavaScriptSerializer jserializer = new JavaScriptSerializer();
36 string filePath = "objPerson.json";
37 Person p;
38 using (FileStream fs = new FileStream(filePath, FileMode.Open))
39 {
40 using (StreamReader sw = new StreamReader(fs, Encoding.Default))
41 {
42 string strJson = sw.ReadToEnd();
43 string strRegex = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
44 MatchEvaluator evaluator = new MatchEvaluator(ConvertDateStringToJsonDate); //對時間停止處置
45 Regex reg = new Regex(strRegex);
46 strJson = reg.Replace(strJson, evaluator);
47 p = jserializer.Deserialize<Person>(strJson);
48 }
49 }
50 SetPersonInfos(p);
51 }
View Code
保管和讀取Bin文檔如下:

1 /// <summary>
2 /// 保管成二進制文件
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void btnSaveBin_Click(object sender, EventArgs e)
7 {
8 Person p = GetPersonInfos();
9 string filePath = "objPerson.bin";
10 using (FileStream fs = new FileStream(filePath, FileMode.Create)) {
11 BinaryFormatter bf = new BinaryFormatter();
12 bf.Serialize(fs, p);
13 }
14 }
15
16 /// <summary>
17 /// 讀取二進制文件
18 /// </summary>
19 /// <param name="sender"></param>
20 /// <param name="e"></param>
21 private void btnReadBin_Click(object sender, EventArgs e)
22 {
23 string filePath = "objPerson.bin";
24 Person p;
25 using (FileStream fs = new FileStream(filePath, FileMode.Open))
26 {
27 BinaryFormatter bf = new BinaryFormatter();
28 p= bf.Deserialize(fs) as Person;
29 }
30 SetPersonInfos(p);
31 }
View Code
備注:其實對象耐久化和對象序列化是兩個不同的概念。兩者有關聯卻不同。
對象耐久化:是使對象可以保管到實體存儲介質中,在對象生命周期完畢後可以再現。
對象序列化:是將對象或許數據構造轉化成特定的格式,使其可在網絡中傳輸,或許可存儲在內存或許文件中。