Csharp/CSharp Tutorial/Class/Class Serialization

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Using the binary reader and writer for psuedo-serialization

<source lang="csharp">using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.rupression; using System.Net; using System.Net.Mail; using System.Runtime.InteropServices; using System.Text;

struct Employee {

   public string FirstName;
   public string LastName;
   public int Extension;
   public string SocialSecurityNumber;
   public bool Salaried;
   public Employee(string firstName, string lastName, int extension, string ssn, bool salaried)
   {
       this.FirstName = firstName;
       this.LastName = lastName;
       this.Extension = extension;
       this.SocialSecurityNumber = ssn;
       this.Salaried = salaried;
   }
   public override string ToString()
   {
       return string.Format("{0}, {1}; {2}; {3}; {4}", LastName, FirstName, Extension, SocialSecurityNumber, Salaried);
   }

} public class MainClass {

   private static List<Employee> CreateEmployees()
   {
       List<Employee> emps = new List<Employee>();
       emps.Add(new Employee("J", "D", 3, "001-21-2232", true));
       emps.Add(new Employee("G", "B", 5, "001-21-0002", true));
       emps.Add(new Employee("J", "R", 9, "322-21-4321", false));
       emps.Add(new Employee("B", "B", 3, "331-22-1211", true));
       emps.Add(new Employee("H", "S", 9, "991-28-2777", false));
       return emps;
   }
   private static List<Employee> DeserializeEmployees(Stream s)
   {
       List<Employee> employees = new List<Employee>();
       BinaryReader reader = new BinaryReader(s);
       try
       {
           while (true)
           {
               Employee e = new Employee();
               e.FirstName = reader.ReadString();
               e.LastName = reader.ReadString();
               e.Extension = reader.ReadInt32();
               e.SocialSecurityNumber = reader.ReadString();
               e.Salaried = reader.ReadBoolean();
               employees.Add(e);
               Console.WriteLine("Read: {0}", e.ToString());
           }
       }
       catch (EndOfStreamException)
       {
       }
       return employees;
   }
   private static void SerializeEmployees(Stream s, IEnumerable<Employee> employees)
   {
       BinaryWriter writer = new BinaryWriter(s);
       foreach (Employee e in employees)
       {
           writer.Write(e.FirstName);
           writer.Write(e.LastName);
           writer.Write(e.Extension);
           writer.Write(e.SocialSecurityNumber);
           writer.Write(e.Salaried);
           Console.WriteLine("Wrote: {0}", e.ToString());
       }
   }
   public static void Main()
   {
       Stream s = new MemoryStream();
       IEnumerable<Employee> employees = CreateEmployees();
       SerializeEmployees(s, employees);
       s.Seek(0, SeekOrigin.Begin);
       DeserializeEmployees(s);
       s.Seek(0, SeekOrigin.Begin);
       int read;
       while ((read = s.ReadByte()) != -1)
           Console.Write("{0:X} ", read);
   }

}</source>

Wrote: D, J; 3; 001-21-2232; True
Wrote: B, G; 5; 001-21-0002; True
Wrote: R, J; 9; 322-21-4321; False
Wrote: B, B; 3; 331-22-1211; True
Wrote: S, H; 9; 991-28-2777; False
Read: D, J; 3; 001-21-2232; True
Read: B, G; 5; 001-21-0002; True
Read: R, J; 9; 322-21-4321; False
Read: B, B; 3; 331-22-1211; True
Read: S, H; 9; 991-28-2777; False
1 4A 1 44 3 0 0 0 B 30 30 31 2D 32 31 2D 32 32 33 32 1 1 47 1 42 5 0 0 0 B 30 30 31 2D 32 31 2D 30 3
0 30 32 1 1 4A 1 52 9 0 0 0 B 33 32 32 2D 32 31 2D 34 33 32 31 0 1 42 1 42 3 0 0 0 B 33 33 31 2D 32
32 2D 31 32 31 31 1 1 48 1 53 9 0 0 0 B 39 39 31 2D 32 38 2D 32 37 37 37 0