Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization.Formatters.Soap;
- using System.Text;
- using System.Xml.Serialization;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create a hashtable of values that will eventually be serialized.
- Hashtable dObj = new Hashtable();
- dObj.Add(1,new Line(100, 100));
- dObj.Add(2,new Circle(50, 10, 10));
- dObj.Add(3,new Square(10, 10, 20, 20));
- dObj.Add(4,new DrawingObject());
- Serialize(dObj);
- Hashtable deserializedList = new Hashtable(Deserialize());
- // display the results
- foreach (DrawingObject DrawingObject in deserializedList.Values)
- {
- switch (DrawingObject.GetType().Name)
- {
- case "Line":
- Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
- Line line = (Line)DrawingObject;
- break;
- case "Circle":
- Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
- Circle circle = (Circle)DrawingObject;
- break;
- case "Square":
- Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
- Square square = (Square)DrawingObject;
- break;
- default:
- Console.WriteLine (string.Format("I'm just a {0}", DrawingObject.Draw()) );
- break;
- }
- }
- Console.ReadLine();
- }
- private static void Serialize(Hashtable EmployeeList)
- {
- // Use a file stream here.
- using (FileStream fs = new FileStream("Test.soap", FileMode.Create))
- {
- // Construct a SoapFormatter and use it
- // to serialize the data to the stream.
- SoapFormatter formatter = new SoapFormatter();
- try
- {
- // Serialize EmployeeList to the file stream
- formatter.Serialize(fs, EmployeeList);
- }
- catch (Exception e)
- {
- Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", e.Message));
- }
- }
- }
- private static Hashtable Deserialize()
- {
- // Ctreate the return Hashtable
- Hashtable EmployeeList2=new Hashtable();
- // Open the file containing the data that you want to deserialize.
- using (FileStream fs = new FileStream("Test.soap", FileMode.Open))
- {
- // Construct a SoapFormatter and use it
- // to serialize the data from the stream.
- SoapFormatter formatter = new SoapFormatter();
- try
- {
- // Deserialize the hashtable from the file
- EmployeeList2 = (Hashtable)formatter.Deserialize(fs);
- }
- catch (Exception e)
- {
- Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", e.Message));
- }
- }
- // return the Deserialized data.
- return EmployeeList2;
- }
- }
- // The object we are going to Serializing/De-serializing
- [XmlInclude(typeof(Line))]
- [XmlInclude(typeof(Circle))]
- [XmlInclude(typeof(Square))]
- [Serializable()]
- public class DrawingObject
- {
- public virtual string Draw()
- {
- return "generic drawing object.";
- }
- }
- [Serializable()]
- public class Square : DrawingObject
- {
- public override string Draw()
- {
- return "Square.";
- }
- public Square()
- {
- }
- public Square(int x1, int y1, int x2, int y2)
- {
- X1 = x1;
- Y1 = y1;
- X2 = x2;
- Y2 = y2;
- }
- public int X1 { get; set; }
- public int Y1 { get; set; }
- public int X2 { get; set; }
- public int Y2 { get; set; }
- }
- [Serializable()]
- public class Line : DrawingObject
- {
- public override string Draw()
- {
- return "Line.";
- }
- public Line()
- {
- }
- public Line(int x, int y)
- {
- this.X = x;
- this.Y = y;
- }
- public int X { get; set; }
- public int Y { get; set; }
- }
- [Serializable()]
- public class Circle : DrawingObject
- {
- public override string Draw()
- {
- return "Circle.";
- }
- public Circle()
- {
- }
- public Circle(int radius, int x, int y)
- {
- this.Radius = radius;
- this.X = x;
- this.Y = y;
- }
- public int Radius { get; set; }
- public int X { get; set; }
- public int Y { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement