Advertisement
NAK

Polymorphism/Serialization DataContractSerializer (C#)

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