1 using System;
2 using System.IO;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Formatters.Binary;
5 [Serializable]
6 public class Insect : ISerializable
7 {
8 private string name;
9 private int id;
10 public Insect(string name, int id)
11 {
12 this.name = name;
13 this.id = id;
14 }
15 public override string ToString()
16 {
17 return String.Format("{0}:{1}", name, id);
18 }
19 public Insect(){}
20
21 public virtual void GetObjectData(SerializationInfo s, StreamingContext c)
22 {
23 s.AddValue("CommonName", name);
24 s.AddValue("ID#", id);
25 }
26 private Insect(SerializationInfo s, StreamingContext c)
27 {
28 name = s.GetString("CommonName");
29 id = s.GetInt32("ID#");
30 }
31 }
32
33 class ImpISerialApp
34 {
35 static void Main(string[] args)
36 {
37 Insect i = new Insect("Meadow Brown", 12);
38 Stream s = File.Create("Insect.bin");
39 BinaryFormatter b = new BinaryFormatter();
40 b.Serialize(s, i);
41 s.Seek(0, SeekOrigin.Begin);
42 Insect j = (Insect)b.Deserialize(s);
43 s.Close();
44 Console.WriteLine(j);
45 return;
46 }
47 }
2 using System.IO;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Formatters.Binary;
5 [Serializable]
6 public class Insect : ISerializable
7 {
8 private string name;
9 private int id;
10 public Insect(string name, int id)
11 {
12 this.name = name;
13 this.id = id;
14 }
15 public override string ToString()
16 {
17 return String.Format("{0}:{1}", name, id);
18 }
19 public Insect(){}
20
21 public virtual void GetObjectData(SerializationInfo s, StreamingContext c)
22 {
23 s.AddValue("CommonName", name);
24 s.AddValue("ID#", id);
25 }
26 private Insect(SerializationInfo s, StreamingContext c)
27 {
28 name = s.GetString("CommonName");
29 id = s.GetInt32("ID#");
30 }
31 }
32
33 class ImpISerialApp
34 {
35 static void Main(string[] args)
36 {
37 Insect i = new Insect("Meadow Brown", 12);
38 Stream s = File.Create("Insect.bin");
39 BinaryFormatter b = new BinaryFormatter();
40 b.Serialize(s, i);
41 s.Seek(0, SeekOrigin.Begin);
42 Insect j = (Insect)b.Deserialize(s);
43 s.Close();
44 Console.WriteLine(j);
45 return;
46 }
47 }
浙公网安备 33010602011771号