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);
}
}