Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace MyObjSerial
- {
- [Serializable()] //Set this attribute to all the classes that you define to be serialized
- public class Employee : ISerializable
- {
- public int EmpId;
- public string EmpName;
- public string EmpFamily;
- public double EmpSalary;
- //Default constructor
- public Employee()
- {
- EmpId = 0;
- EmpName = "";
- EmpFamily = "Jones";
- EmpSalary = 100.00;
- }
- //Deserialization constructor.
- public Employee(SerializationInfo info, StreamingContext ctxt)
- {
- //Get the values from info and assign them to the appropriate properties
- EmpId = (int)info.GetValue("EmployeeId", typeof(int));
- EmpName = (String)info.GetValue("EmployeeName", typeof(string));
- EmpFamily = (String)info.GetValue("EmployeeFamily", typeof(string));
- EmpSalary = (double)info.GetValue("EmployeeSalary", typeof(double));
- }
- //Serialization function.
- public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
- {
- //You can use any custom name for your name-value pair. But make sure you
- // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
- // then you should read the same with "EmployeeId"
- info.AddValue("EmployeeId", EmpId);
- info.AddValue("EmployeeName", EmpName);
- info.AddValue("EmployeeFamily", EmpFamily);
- info.AddValue("EmployeeSalary", EmpSalary);
- }
- }
- //Main class
- public class ObjSerial
- {
- static void Main(String[] args)
- {
- Employee mp = new Employee();
- StringBuilder sb = new StringBuilder();
- sb.Append("First Line");
- sb.Append("\n");
- sb.Append("Second Line");
- Console.WriteLine(sb.ToString());
- int i = 0;
- for (i = 0; i < 10; i++)
- {
- mp.EmpId = i;
- mp.EmpFamily = "Jones";
- mp.EmpSalary = 10.10;
- if (RandomNumber.rnd.NextDouble() <= 1.0 / 5.0)
- {
- string RandStr = StringGenerator.RandomString(6);
- mp.EmpName = RandStr;
- }
- else if (RandomNumber.rnd.NextDouble() <= 2.0 / 5.0)
- {
- string RandStr = StringGenerator.RandomString(7);
- mp.EmpName = RandStr;
- }
- else if (RandomNumber.rnd.NextDouble() <= 3.0 / 5.0)
- {
- string RandStr = StringGenerator.RandomString(8);
- mp.EmpName = RandStr;
- }
- else if (RandomNumber.rnd.NextDouble() <= 5.0 / 5.0)
- {
- string RandStr = StringGenerator.RandomString(9);
- mp.EmpName = RandStr;
- }
- else if (RandomNumber.rnd.NextDouble() <= 5.0 / 5.0)
- {
- string RandStr = StringGenerator.RandomString(10);
- mp.EmpName = RandStr;
- }
- Console.ReadLine();
- // Open a file and serialize the object into it in binary format.
- // EmployeeInfo.osl is the file that we are creating.
- // Note:- you can give any extension you want for your file
- // If you use custom extensions, then the user will now
- // that the file is associated with your program.
- Stream stream = File.Open("EmployeeInfo.txt", FileMode.Append);
- BinaryFormatter bformatter = new BinaryFormatter();
- Console.WriteLine("Writing Employee Information");
- bformatter.Serialize(stream, mp);
- stream.Close();
- //Clear mp for further usage.
- mp = null;
- //Open the file written above and read values from it.
- stream = File.Open("EmployeeInfo.txt", FileMode.Open);
- bformatter = new BinaryFormatter();
- Console.WriteLine("Reading Employee Information");
- mp = (Employee)bformatter.Deserialize(stream);
- stream.Close();
- Console.WriteLine("Employee Id: {0}", mp.EmpId.ToString());
- Console.WriteLine("Employee Name: {0}", mp.EmpName);
- Console.WriteLine("Employee Family: {0}", mp.EmpFamily);
- Console.WriteLine("Employee Salary: {0:C}", mp.EmpSalary);
- Console.ReadLine();
- }
- }
- public class StringGenerator
- {
- static private Random random = new Random();
- static public string RandomString(int size)
- {
- StringBuilder builder = new StringBuilder();
- char ch;
- for (int i = 0; i < size; i++)
- {
- ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
- builder.Append(ch);
- }
- return builder.ToString();
- }
- }
- public class RandomNumber
- {
- public static Random rnd = new Random();
- static RandomNumber()
- {
- for (int i = 0; i < 50000; i++)
- {
- rnd.NextDouble();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment