Guest User

Untitled

a guest
Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7.  
  8. namespace MyObjSerial
  9. {
  10.     [Serializable()]    //Set this attribute to all the classes that you define to be serialized
  11.     public class Employee : ISerializable
  12.     {
  13.         public int EmpId;
  14.         public string EmpName;
  15.         public string EmpFamily;
  16.         public double EmpSalary;
  17.  
  18.         //Default constructor
  19.         public Employee()
  20.         {
  21.             EmpId = 0;
  22.             EmpName = "";
  23.             EmpFamily = "Jones";
  24.             EmpSalary = 100.00;
  25.         }
  26.  
  27.         //Deserialization constructor.
  28.         public Employee(SerializationInfo info, StreamingContext ctxt)
  29.         {
  30.             //Get the values from info and assign them to the appropriate properties
  31.             EmpId = (int)info.GetValue("EmployeeId", typeof(int));
  32.             EmpName = (String)info.GetValue("EmployeeName", typeof(string));
  33.             EmpFamily = (String)info.GetValue("EmployeeFamily", typeof(string));
  34.             EmpSalary = (double)info.GetValue("EmployeeSalary", typeof(double));
  35.         }
  36.  
  37.         //Serialization function.
  38.         public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
  39.         {
  40.             //You can use any custom name for your name-value pair. But make sure you
  41.             // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
  42.             // then you should read the same with "EmployeeId"
  43.             info.AddValue("EmployeeId", EmpId);
  44.             info.AddValue("EmployeeName", EmpName);
  45.             info.AddValue("EmployeeFamily", EmpFamily);
  46.             info.AddValue("EmployeeSalary", EmpSalary);
  47.         }
  48.     }
  49.  
  50.     //Main class
  51.     public class ObjSerial
  52.     {
  53.         static void Main(String[] args)
  54.         {
  55.  
  56.  
  57.  
  58.  
  59.  
  60.             Employee mp = new Employee();
  61.  
  62.  
  63.             StringBuilder sb = new StringBuilder();
  64.             sb.Append("First Line");
  65.             sb.Append("\n");
  66.             sb.Append("Second Line");
  67.             Console.WriteLine(sb.ToString());
  68.             int i = 0;
  69.             for (i = 0; i < 10; i++)
  70.             {
  71.                 mp.EmpId = i;
  72.                 mp.EmpFamily = "Jones";
  73.                 mp.EmpSalary = 10.10;
  74.                 if (RandomNumber.rnd.NextDouble() <= 1.0 / 5.0)
  75.                 {
  76.                     string RandStr = StringGenerator.RandomString(6);
  77.                     mp.EmpName = RandStr;
  78.                 }
  79.                 else if (RandomNumber.rnd.NextDouble() <= 2.0 / 5.0)
  80.                 {
  81.                     string RandStr = StringGenerator.RandomString(7);
  82.                     mp.EmpName = RandStr;
  83.                 }
  84.                 else if (RandomNumber.rnd.NextDouble() <= 3.0 / 5.0)
  85.                 {
  86.                     string RandStr = StringGenerator.RandomString(8);
  87.                     mp.EmpName = RandStr;
  88.                 }
  89.                 else if (RandomNumber.rnd.NextDouble() <= 5.0 / 5.0)
  90.                 {
  91.                     string RandStr = StringGenerator.RandomString(9);
  92.                     mp.EmpName = RandStr;
  93.                 }
  94.                 else if (RandomNumber.rnd.NextDouble() <= 5.0 / 5.0)
  95.                 {
  96.                     string RandStr = StringGenerator.RandomString(10);
  97.                     mp.EmpName = RandStr;
  98.                 }
  99.  
  100.                 Console.ReadLine();
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.                 // Open a file and serialize the object into it in binary format.
  112.                 // EmployeeInfo.osl is the file that we are creating.
  113.                 // Note:- you can give any extension you want for your file
  114.                 // If you use custom extensions, then the user will now
  115.                 //   that the file is associated with your program.
  116.                 Stream stream = File.Open("EmployeeInfo.txt", FileMode.Append);
  117.                 BinaryFormatter bformatter = new BinaryFormatter();
  118.  
  119.                 Console.WriteLine("Writing Employee Information");
  120.  
  121.                 bformatter.Serialize(stream, mp);
  122.                 stream.Close();
  123.  
  124.                 //Clear mp for further usage.
  125.                 mp = null;
  126.  
  127.  
  128.                 //Open the file written above and read values from it.
  129.                 stream = File.Open("EmployeeInfo.txt", FileMode.Open);
  130.  
  131.                 bformatter = new BinaryFormatter();
  132.  
  133.                 Console.WriteLine("Reading Employee Information");
  134.                 mp = (Employee)bformatter.Deserialize(stream);
  135.                 stream.Close();
  136.                 Console.WriteLine("Employee Id: {0}", mp.EmpId.ToString());
  137.                 Console.WriteLine("Employee Name: {0}", mp.EmpName);
  138.                 Console.WriteLine("Employee Family: {0}", mp.EmpFamily);
  139.                 Console.WriteLine("Employee Salary: {0:C}", mp.EmpSalary);
  140.                 Console.ReadLine();
  141.  
  142.             }
  143.  
  144.         }
  145.  
  146.  
  147.  
  148.         public class StringGenerator
  149.         {
  150.             static private Random random = new Random();
  151.             static public string RandomString(int size)
  152.             {
  153.                 StringBuilder builder = new StringBuilder();
  154.                 char ch;
  155.                 for (int i = 0; i < size; i++)
  156.                 {
  157.                     ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
  158.                     builder.Append(ch);
  159.                 }
  160.                 return builder.ToString();
  161.             }
  162.         }
  163.         public class RandomNumber
  164.         {
  165.             public static Random rnd = new Random();
  166.             static RandomNumber()
  167.             {
  168.                 for (int i = 0; i < 50000; i++)
  169.                 {
  170.                     rnd.NextDouble();
  171.                 }
  172.             }
  173.         }
  174.     }
  175. }
Add Comment
Please, Sign In to add comment