Advertisement
NAK

Polymorphism/Serialization XmlSerializer (C#)

NAK
Dec 10th, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml.Serialization;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     class Program
  11.     {
  12.         static void 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.             Serialize(dObj);
  23.             List<DrawingObject> deserializedList = new List<DrawingObject>();
  24.             // deserialize to a generic list
  25.             deserializedList = Deserialize<List<DrawingObject>>();
  26.  
  27.             // display the results
  28.             foreach (DrawingObject DrawingObject in deserializedList)
  29.             {
  30.                 switch (DrawingObject.GetType().Name)
  31.                 {
  32.                     case "Line":
  33.                         Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
  34.                         Line line = (Line)DrawingObject;
  35.                         break;
  36.                     case "Circle":
  37.                         Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
  38.                         Circle circle = (Circle)DrawingObject;
  39.                         break;
  40.                     case "Square":
  41.                         Console.WriteLine (string.Format("I'm a {0}", DrawingObject.Draw()) );
  42.                         Square square = (Square)DrawingObject;
  43.                         break;
  44.                     default:
  45.                         Console.WriteLine (string.Format("I'm just a {0}", DrawingObject.Draw()) );
  46.                         break;
  47.                 }
  48.             }
  49.             Console.ReadLine();
  50.         }
  51.  
  52.         private static void Serialize<T>(T data)
  53.         {
  54.  
  55.             // Use a file stream here.
  56.             using (TextWriter WriteFileStream = new StreamWriter("test.xml"))
  57.             {
  58.                 // Construct a SoapFormatter and use it  
  59.                 // to serialize the data to the stream.
  60.                 XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
  61.  
  62.                 try
  63.                 {
  64.                     // Serialize EmployeeList to the file stream
  65.                     SerializerObj.Serialize(WriteFileStream, data);
  66.                 }
  67.                 catch (Exception ex)
  68.                 {
  69.                     Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
  70.                 }
  71.             }
  72.         }
  73.  
  74.         private static T Deserialize<T>() where T : new()
  75.         {
  76.             //List<Employee> EmployeeList2 = new List<Employee>();
  77.             // Create an instance of T
  78.             T ReturnListOfT = CreateInstance<T>();
  79.  
  80.  
  81.            // Create a new file stream for reading the XML file
  82.             using (FileStream ReadFileStream = new FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
  83.             {
  84.                 // Construct a XmlSerializer and use it  
  85.                 // to serialize the data from the stream.
  86.                 XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
  87.                 try
  88.                 {
  89.                     // Deserialize the hashtable from the file
  90.                     ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
  91.                 }
  92.                 catch (Exception ex)
  93.                 {
  94.                     Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
  95.                 }
  96.  
  97.             }
  98.             // return the Deserialized data.
  99.             return ReturnListOfT;
  100.         }
  101.  
  102.         // function to create instance of T
  103.         public static T CreateInstance<T>() where T : new()
  104.         {
  105.             return (T)Activator.CreateInstance(typeof(T));
  106.         }
  107.  
  108.     }
  109.  
  110.     // The object we are going to Serializing/De-serializing
  111.     [XmlInclude(typeof(Line))]
  112.     [XmlInclude(typeof(Circle))]
  113.     [XmlInclude(typeof(Square))]
  114.         [Serializable()]
  115.     public class DrawingObject
  116.     {
  117.         public virtual string Draw()
  118.         {
  119.             return "generic drawing object.";
  120.         }
  121.     }
  122.      
  123.     [Serializable()]
  124.     public class Square : DrawingObject
  125.     {
  126.         public override string Draw()
  127.         {
  128.             return "Square.";
  129.         }
  130.      
  131.         public Square()
  132.         {
  133.         }
  134.      
  135.         public Square(int x1, int y1, int x2, int y2)
  136.         {
  137.             X1 = x1;
  138.             Y1 = y1;
  139.             X2 = x2;
  140.             Y2 = y2;
  141.         }
  142.      
  143.         public int X1 { get; set; }
  144.         public int Y1 { get; set; }
  145.      
  146.         public int X2 { get; set; }
  147.         public int Y2 { get; set; }
  148.     }
  149.      
  150.     [Serializable()]
  151.     public class Line : DrawingObject
  152.     {
  153.         public override string Draw()
  154.         {
  155.             return "Line.";
  156.         }
  157.      
  158.         public Line()
  159.         {
  160.         }
  161.      
  162.         public Line(int x, int y)
  163.         {
  164.             this.X = x;
  165.             this.Y = y;
  166.         }
  167.      
  168.         public int X { get; set; }
  169.         public int Y { get; set; }
  170.     }
  171.      
  172.     [Serializable()]
  173.     public class Circle : DrawingObject
  174.     {
  175.         public override string Draw()
  176.         {
  177.             return "Circle.";
  178.         }
  179.      
  180.         public Circle()
  181.         {
  182.         }
  183.      
  184.         public Circle(int radius, int x, int y)
  185.         {
  186.             this.Radius = radius;
  187.             this.X = x;
  188.             this.Y = y;
  189.         }
  190.      
  191.         public int Radius { get; set; }
  192.         public int X { get; set; }
  193.         public int Y { get; set; }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement