c#序列化入门
Code
1/**//*****************序列化与反序列化***************
2 * 好文:http://www.cnblogs.com/chjw8016/archive/2008/02/23/1078204.html
3 * 1.把对象转换为字节序列的过程称为对象的序列化。
4 * 2.把字节序列恢复为对象的过程称为对象的反序列化。
5 * 3.最简单的方法是使用 Serializable 属性对类进行标记
6 * 4.IFormatter提供序列化的接口
7 ************************************************/
8using System;
9using System.Data;
10using System.Configuration;
11using System.Web;
12using System.Web.Security;
13using System.Web.UI;
14using System.Web.UI.WebControls;
15using System.Web.UI.WebControls.WebParts;
16using System.Web.UI.HtmlControls;
17
18/**////
19/// MyObject 的摘要说明
20///
21[Serializable]
22public class MyObject
23{
24 public int n1 = 0;
25 public int n2 = 0;
26 public string str = null;
27 public MyObject()
28 {
29 //
30 // TODO: 在此处添加构造函数逻辑
31 //
32 }
33}
34Default.cs
35
36using System;
37using System.Data;
38using System.Configuration;
39using System.Web;
40using System.Web.Security;
41using System.Web.UI;
42using System.Web.UI.WebControls;
43using System.Web.UI.WebControls.WebParts;
44using System.Web.UI.HtmlControls;
45
46using System.IO;
47using System.Runtime.Serialization;
48using System.Runtime.Serialization.Formatters.Binary;
49
50public partial class _Default : System.Web.UI.Page
51{
52 protected void Page_Load(object sender, EventArgs e)
53 {
54 //FuncSerialize();
55 FuncDeserialize();
56 }
57 /**////
58 /// 序列化,把对象序列化为一个文件
59 ///
60 private void FuncSerialize()
61 {
62 MyObject obj = new MyObject();
63 obj.n1 = 1;
64 obj.n2 = 24;
65 obj.str = "字符串";
66 IFormatter formatter = new BinaryFormatter();
67 Stream stream = new FileStream(@"c:\MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
68 formatter.Serialize(stream, obj);
69 stream.Close();
70 }
71 /**////
72 /// 反序列化,把文件化为一个对象
73 ///
74 private void FuncDeserialize()
75 {
76 IFormatter formatter = new BinaryFormatter();
77 Stream stream = new FileStream(@"c:\MyFile.bin", FileMode.Open,
78 FileAccess.Read, FileShare.Read);
79 MyObject obj = (MyObject)formatter.Deserialize(stream);
80 stream.Close();
81 this.Title = obj.str;
82 }
83}
84
1/**//*****************序列化与反序列化***************
2 * 好文:http://www.cnblogs.com/chjw8016/archive/2008/02/23/1078204.html
3 * 1.把对象转换为字节序列的过程称为对象的序列化。
4 * 2.把字节序列恢复为对象的过程称为对象的反序列化。
5 * 3.最简单的方法是使用 Serializable 属性对类进行标记
6 * 4.IFormatter提供序列化的接口
7 ************************************************/
8using System;
9using System.Data;
10using System.Configuration;
11using System.Web;
12using System.Web.Security;
13using System.Web.UI;
14using System.Web.UI.WebControls;
15using System.Web.UI.WebControls.WebParts;
16using System.Web.UI.HtmlControls;
17
18/**////
19/// MyObject 的摘要说明
20///
21[Serializable]
22public class MyObject
23{
24 public int n1 = 0;
25 public int n2 = 0;
26 public string str = null;
27 public MyObject()
28 {
29 //
30 // TODO: 在此处添加构造函数逻辑
31 //
32 }
33}
34Default.cs
35
36using System;
37using System.Data;
38using System.Configuration;
39using System.Web;
40using System.Web.Security;
41using System.Web.UI;
42using System.Web.UI.WebControls;
43using System.Web.UI.WebControls.WebParts;
44using System.Web.UI.HtmlControls;
45
46using System.IO;
47using System.Runtime.Serialization;
48using System.Runtime.Serialization.Formatters.Binary;
49
50public partial class _Default : System.Web.UI.Page
51{
52 protected void Page_Load(object sender, EventArgs e)
53 {
54 //FuncSerialize();
55 FuncDeserialize();
56 }
57 /**////
58 /// 序列化,把对象序列化为一个文件
59 ///
60 private void FuncSerialize()
61 {
62 MyObject obj = new MyObject();
63 obj.n1 = 1;
64 obj.n2 = 24;
65 obj.str = "字符串";
66 IFormatter formatter = new BinaryFormatter();
67 Stream stream = new FileStream(@"c:\MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
68 formatter.Serialize(stream, obj);
69 stream.Close();
70 }
71 /**////
72 /// 反序列化,把文件化为一个对象
73 ///
74 private void FuncDeserialize()
75 {
76 IFormatter formatter = new BinaryFormatter();
77 Stream stream = new FileStream(@"c:\MyFile.bin", FileMode.Open,
78 FileAccess.Read, FileShare.Read);
79 MyObject obj = (MyObject)formatter.Deserialize(stream);
80 stream.Close();
81 this.Title = obj.str;
82 }
83}
84
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。