Advertisement
NAK

Polymorphism/Serialization BinaryFormatter(C#)

NAK
Dec 2nd, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.Serialization;
  5. using System.IO;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7.      
  8.     namespace Polymorphism
  9.     {
  10.         class Program
  11.         {
  12.             static int Main(string[] args)
  13.             {
  14.                 // Create some objects
  15.                 List<DrawingObject> dObj = new List<DrawingObject>();
  16.  
  17.                 dObj.Add(new Line(100, 100));
  18.                 dObj.Add(new Circle(50,10,10));
  19.                 dObj.Add(new Square(10,10,20,20));
  20.                 dObj.Add(new DrawingObject());
  21.      
  22.                 foreach (DrawingObject drawObj in dObj)
  23.                 {
  24.                     switch (drawObj.GetType().Name)
  25.                     {
  26.                         case "Line":
  27.                             Console.WriteLine (string.Format("I'm a {0}", drawObj.Draw()) );
  28.                             Line line = (Line)drawObj;
  29.                             break;
  30.                         case "Circle":
  31.                             Console.WriteLine (string.Format("I'm a {0}", drawObj.Draw()) );
  32.                             Circle circle = (Circle)drawObj;
  33.                             break;
  34.                         case "Square":
  35.                             Console.WriteLine (string.Format("I'm a {0}", drawObj.Draw()) );
  36.                             Square square = (Square)drawObj;
  37.                             break;
  38.                         default:
  39.                             Console.WriteLine (string.Format("I'm just a {0}", drawObj.Draw()) );
  40.                             break;
  41.                     }
  42.                     drawObj.Draw();
  43.                 }
  44.      
  45.                 // Serialize
  46.                 Serialize(dObj);
  47.                
  48.                 List<DrawingObject> deserializedList = new List<DrawingObject>();
  49.                 // deserialize to a generic list
  50.                 deserializedList = Deserialize<List<DrawingObject>>();
  51.      
  52.                 foreach (DrawingObject DrawingObject in deserializedList)
  53.                 {
  54.                     switch (DrawingObject.GetType().Name)
  55.                     {
  56.                         case "Line":
  57.                             Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
  58.                             Line line = (Line)DrawingObject;
  59.                             break;
  60.                         case "Circle":
  61.                             Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
  62.                             Circle circle = (Circle)DrawingObject;
  63.                             break;
  64.                         case "Square":
  65.                             Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
  66.                             Square square = (Square)DrawingObject;
  67.                             break;
  68.                         default:
  69.                             Console.WriteLine (string.Format("I'm just a {0}", DrawingObject.Draw()) );
  70.                             break;
  71.                     }
  72.                 }
  73.      
  74.                 return 0;
  75.             }
  76.      
  77.             public static void Serialize<T>(T data)
  78.             {
  79.                 try // try to serialize the collection to a file
  80.                 {
  81.                     using (Stream  stream = File.Open("data.bin", FileMode.Create))
  82.                     {
  83.                         // create BinaryFormatter
  84.                         BinaryFormatter bin = new BinaryFormatter();
  85.                         // serialize the collection (EmployeeList1) to file (stream)
  86.                         bin.Serialize(stream, data);
  87.                     }
  88.                 }
  89.                 catch (IOException)
  90.                 {
  91.                 }
  92.             }
  93.      
  94.             public static T Deserialize<T>() where T : new()
  95.             {
  96.                 // Create an instance of T
  97.                 T ReturnListOfT = CreateInstance<T>();
  98.      
  99.                 // Try to Deserialize from file stream
  100.                 try
  101.                 {
  102.                     using (Stream stream = File.Open("data.bin", FileMode.Open))
  103.                     {
  104.                         // create BinaryFormatter
  105.                         BinaryFormatter bin = new BinaryFormatter();
  106.                         // deserialize the collection (Employee) from file (stream)
  107.                         ReturnListOfT = (T)bin.Deserialize(stream);
  108.                     }
  109.                 }
  110.                 catch (IOException)
  111.                 {
  112.                 }
  113.      
  114.                 return (T)ReturnListOfT;
  115.             }
  116.      
  117.             // function to create instance of T
  118.             public static T CreateInstance<T>() where T : new()
  119.             {
  120.                 return (T)Activator.CreateInstance(typeof(T));
  121.             }
  122.         }
  123.      
  124.         [KnownType(typeof(Line))]
  125.         [KnownType(typeof(Circle))]
  126.         [KnownType(typeof(Square))]
  127.         [Serializable()]
  128.         class DrawingObject
  129.         {
  130.             public virtual string Draw()
  131.             {
  132.                 return "generic drawing object.";
  133.             }
  134.         }
  135.      
  136.         [Serializable()]
  137.         class Square : DrawingObject
  138.         {
  139.             public override string Draw()
  140.             {
  141.                 return "Square.";
  142.             }
  143.      
  144.             public Square()
  145.             {
  146.             }
  147.      
  148.             public Square(int x1, int y1, int x2, int y2)
  149.             {
  150.                 X1 = x1;
  151.                 Y1 = y1;
  152.                 X2 = x2;
  153.                 Y2 = y2;
  154.             }
  155.      
  156.             public int X1 { get; set; }
  157.             public int Y1 { get; set; }
  158.      
  159.             public int X2 { get; set; }
  160.             public int Y2 { get; set; }
  161.         }
  162.      
  163.         [Serializable()]
  164.         class Line : DrawingObject
  165.         {
  166.             public override string Draw()
  167.             {
  168.                 return "Line.";
  169.             }
  170.      
  171.             public Line()
  172.             {
  173.             }
  174.      
  175.             public Line(int x, int y)
  176.             {
  177.                 this.X = x;
  178.                 this.Y = y;
  179.             }
  180.      
  181.             public int X { get; set; }
  182.             public int Y { get; set; }
  183.         }
  184.      
  185.         [Serializable()]
  186.         class Circle : DrawingObject
  187.         {
  188.             public override string Draw()
  189.             {
  190.                 return "Circle.";
  191.             }
  192.      
  193.             public Circle()
  194.             {
  195.             }
  196.      
  197.             public Circle(int radius, int x, int y)
  198.             {
  199.                 this.Radius = radius;
  200.                 this.X = x;
  201.                 this.Y = y;
  202.             }
  203.      
  204.             public int Radius { get; set; }
  205.             public int X { get; set; }
  206.             public int Y { get; set; }
  207.         }
  208.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement