Advertisement
NAK

Polymorphism/Serialization SoapFormatter (C#)

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