序列化程序如下:- class Program
- {
- static void Main(string[] args)
- {
- BinaryFormatter bf1 = new BinaryFormatter();
- FileStream fs1=new FileStream("1.bat",FileMode.Create,FileAccess.Write);
- using (fs1)
- {
- Person p1 = new Person("wl", 18, '男');
- bf1.Serialize(fs1, p1);
- }
-
- }
- }
- [Serializable]
- class Person
- {
- private string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- private int age;
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- private char gender;
- public char Gender
- {
- get { return gender; }
- set { gender = value; }
- }
- public Person(string name,int age,char gender)
- {
- this.name = name;
- this.age = age;
- this.gender = gender;
- }
- public void SayHello()
- {
- Console.WriteLine("大家好,我叫{0},今年{1},性别{2}",Name,Age,Gender);
- }
-
- }
复制代码 反序列化程序- class Program
- {
- static void Main(string[] args)
- {
- BinaryFormatter bf1 = new BinaryFormatter();
- Person p1;
- using (FileStream fs1 = new FileStream("1.bat", FileMode.Open, FileAccess.Read))
- {
- p1 =(Person) bf1.Deserialize(fs1);
- }
-
-
- }
- }
- [Serializable]
- class Person
- {
- private string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- private int age;
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- private char gender;
- public char Gender
- {
- get { return gender; }
- set { gender = value; }
- }
- public Person(string name, int age, char gender)
- {
- this.name = name;
- this.age = age;
- this.gender = gender;
- }
- public void SayHello()
- {
- Console.WriteLine("大家好,我叫{0},今年{1},性别{2}", Name, Age, Gender);
- }
- }
复制代码 运行反序列化后,怎么报错?
|
|