Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.Serialization;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace Polymorphism
- {
- class Program
- {
- static int Main(string[] args)
- {
- // Create some objects
- List<DrawingObject> dObj = new List<DrawingObject>();
- dObj.Add(new Line(100, 100));
- dObj.Add(new Circle(50,10,10));
- dObj.Add(new Square(10,10,20,20));
- dObj.Add(new DrawingObject());
- foreach (DrawingObject drawObj in dObj)
- {
- switch (drawObj.GetType().Name)
- {
- case "Line":
- Console.WriteLine (string.Format("I'm a {0}", drawObj.Draw()) );
- Line line = (Line)drawObj;
- break;
- case "Circle":
- Console.WriteLine (string.Format("I'm a {0}", drawObj.Draw()) );
- Circle circle = (Circle)drawObj;
- break;
- case "Square":
- Console.WriteLine (string.Format("I'm a {0}", drawObj.Draw()) );
- Square square = (Square)drawObj;
- break;
- default:
- Console.WriteLine (string.Format("I'm just a {0}", drawObj.Draw()) );
- break;
- }
- drawObj.Draw();
- }
- // Serialize
- Serialize(dObj);
- List<DrawingObject> deserializedList = new List<DrawingObject>();
- // deserialize to a generic list
- deserializedList = Deserialize<List<DrawingObject>>();
- foreach (DrawingObject DrawingObject in deserializedList)
- {
- 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;
- }
- }
- return 0;
- }
- public static void Serialize<T>(T data)
- {
- try // try to serialize the collection to a file
- {
- using (Stream stream = File.Open("data.bin", FileMode.Create))
- {
- // create BinaryFormatter
- BinaryFormatter bin = new BinaryFormatter();
- // serialize the collection (EmployeeList1) to file (stream)
- bin.Serialize(stream, data);
- }
- }
- catch (IOException)
- {
- }
- }
- public static T Deserialize<T>() where T : new()
- {
- // Create an instance of T
- T ReturnListOfT = CreateInstance<T>();
- // Try to Deserialize from file stream
- try
- {
- using (Stream stream = File.Open("data.bin", FileMode.Open))
- {
- // create BinaryFormatter
- BinaryFormatter bin = new BinaryFormatter();
- // deserialize the collection (Employee) from file (stream)
- ReturnListOfT = (T)bin.Deserialize(stream);
- }
- }
- catch (IOException)
- {
- }
- return (T)ReturnListOfT;
- }
- // function to create instance of T
- public static T CreateInstance<T>() where T : new()
- {
- return (T)Activator.CreateInstance(typeof(T));
- }
- }
- [KnownType(typeof(Line))]
- [KnownType(typeof(Circle))]
- [KnownType(typeof(Square))]
- [Serializable()]
- class DrawingObject
- {
- public virtual string Draw()
- {
- return "generic drawing object.";
- }
- }
- [Serializable()]
- 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()]
- 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()]
- 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