您的位置:首页 > 其它

制作不用密码可立即登入的 ssh 用户

2012-02-08 03:59 351 查看
1序列化:是将对象的状态存储到特定的存储介质中的过程,在序列化的过程 中,会将对象的公有成员,私有成员包括类名,都转换成数据流的形式,存储到存储介质中,
2
3反序列化: 是从将特定存储介质中闺怨数据重新构建对象的过程.通过反序列化,可以将庆文件上的对象信息读取,然后重新构建为对象.
4
5一个类要实现序列化,这个类的特性必须被标识为[Serializable]
6
7序列化保存到存储介质上的文件为二进制文件,因此要对二进制进行操作,要引入命名空间:
8
9using System.Runtime.Seriialization.Formatters.Binary;
10
11对二进制操作,就要用到流,要引入命名空间
12
13using System.IO;
14
15下面是实例
16
17using System;
18using System.Collections.Generic;
19using System.Text;
20
21namespace SerializableTest
22{
23
24    [Serializable] //特性,表明此类可以被序列化
25    public class Students
26    {
27        private string name;
28
29        public string Name
30        {
31            get { return name; }
32            set { name = value; }
33        }
34        private int age;
35
36        public int Age
37        {
38            get { return age; }
39            set { age = value; }
40        }
41
42    }
43}
44
45
46
47using System;
48using System.Collections.Generic;
49using System.Text;
50using System.IO;
51using System.Runtime.Serialization.Formatters.Binary;
52
53namespace SerializableTest
54{
55    /**//// <summary>
56    /// 序列化的类
57    /// </summary>
58   public class SerializableClass
59    {
60       /**//// <summary>
61       /// 序列化学生类的信息
62       /// </summary>
63       /// <param name="student"></param>
64       public void Serial(Students student)
65       {
66           FileStream fs = new FileStream(@"c:\test.bin", FileMode.OpenOrCreate);
67           BinaryFormatter bf = new BinaryFormatter();
68           bf.Serialize(fs, student);
69           fs.Close();
70       }
71
72        /**//// <summary>
73        /// 反序列化学生类的信息
74        /// </summary>
75        /// <returns></returns>
76
77       public Students DeSerial()
78       {
79           FileStream fs = new FileStream(@"c:\test.bin", FileMode.OpenOrCreate);
80           BinaryFormatter bf = new BinaryFormatter();
81          Students stu = bf.Deserialize(fs) as Students;
82          fs.Close();
83          return stu;
84
85       }
86
87       /**//// <summary>
88       /// 序列化泛型集合
89       /// </summary>
90       /// <param name="stuList"></param>
91       public void SerialList(List<Students> stuList)
92       {
93
94               FileStream fs = new FileStream(@"c:\test.txt", FileMode.OpenOrCreate);
95               BinaryFormatter bf = new BinaryFormatter();
96
97               bf.Serialize(fs, stuList);
98
99               fs.Close();
100
101       }
102
103       /**//// <summary>
104       /// 反序列化泛型集合
105       /// </summary>
106       /// <returns></returns>
107       public List<Students> DeSerialList()
108       {
109           FileStream fs = new FileStream(@"c:\test.txt", FileMode.OpenOrCreate);
110           BinaryFormatter bf = new BinaryFormatter();
111           List<Students> stuList = bf.Deserialize(fs) as List<Students>;
112           fs.Close();
113           return stuList;
114       }
115
116
117    }
118}
119
120
121using System;
122using System.Collections.Generic;
123using System.Text;
124
125
126namespace SerializableTest
127{
128    class Program
129    {
130        static void Main(string[] args)
131        {
132
133            SerializableClass sc = new SerializableClass();
134            Students student = new Students();
135            student.Name = "miaoqing";
136            student.Age = 24;
137
138            //调用对类序列化的方法
139            sc.Serial(student);
140            //调用对类反序列化的方法
141            Students stuDe = sc.DeSerial() as Students;
142            Console.WriteLine("对Student类的序列化的输出");
143            Console.WriteLine(stuDe.Name + stuDe.Age);
144
145            //Student 类 泛型集合
146            List<Students> stuList = new List<Students>();
147
148            Students stu = new Students();
149            stu.Name = "sofie";
150            stu.Age = 21;
151            stuList.Add(stu);
152
153            Students stu1 = new Students();
154            stu1.Name = "jrian";
155            stu1.Age = 26;
156            stuList.Add(stu1);
157
158            //调用对泛型序列化的方法
159            sc.SerialList(stuList);
160
161            //调用对泛型反序列化的方法
162            List<Students> stuList1 = sc.DeSerialList();
163            foreach (object obj in stuList1)
164            {
165                Students stuObj = obj as Students;
166                Console.WriteLine(stuObj.Name + stuObj.Age);
167            }
168        }
169    }
170
171}

 /article/5964078.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: