Advertisement
NAK

Polymorphism/Serialization XmlSerializer (VB.NET)

NAK
Dec 10th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.90 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Collections
  3. Imports System.Runtime.Serialization.Formatters.Binary
  4. Imports System.Xml.Serialization
  5.  
  6. Module Module1
  7.  
  8.     Sub Main()
  9.         'Create some objects
  10.         Dim dObj() = New DrawingObject(3) {}
  11.  
  12.         dObj(0) = New Line(100, 100)
  13.         dObj(1) = New Circle(50, 10, 10)
  14.         dObj(2) = New Square(10, 10, 20, 20)
  15.         dObj(3) = New DrawingObject()
  16.  
  17.         Serialize(dObj)
  18.  
  19.         Dim deserializedList = New List(Of DrawingObject)()
  20.         ' deserialize to a generic list
  21.         deserializedList = Deserialize(Of List(Of DrawingObject))()
  22.  
  23.         ' display the results
  24.         For Each DrawingObject As DrawingObject In deserializedList
  25.  
  26.             Select Case DrawingObject.GetType().Name
  27.                 Case "Line"
  28.                     Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
  29.                     Dim line As Line = DirectCast(DrawingObject, Line)
  30.                 Case "Circle"
  31.                     Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
  32.                     Dim circle As Circle = DirectCast(DrawingObject, Circle)
  33.                 Case "Square"
  34.                     Dim square As Square = DirectCast(DrawingObject, Square)
  35.                     Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
  36.                 Case Else
  37.                     Console.WriteLine(String.Format("I'm just a {0}", DrawingObject.Draw()))
  38.             End Select
  39.         Next
  40.  
  41.         Console.ReadLine()
  42.     End Sub
  43.  
  44.     Private Sub Serialize(Of T)(data As T)
  45.  
  46.         ' Use a file stream here.
  47.         Using WriteFileStream As New StreamWriter("test.xml")
  48.             ' Construct a SoapFormatter and use it  
  49.             ' to serialize the data to the stream.
  50.             Dim SerializerObj As New XmlSerializer(GetType(DrawingObject()))
  51.  
  52.             Try
  53.                 ' Serialize EmployeeList to the file stream
  54.                 SerializerObj.Serialize(WriteFileStream, data)
  55.             Catch ex As Exception
  56.                 Console.WriteLine(String.Format("Failed to serialize. Reason: {0}", ex.Message))
  57.             End Try
  58.         End Using
  59.     End Sub
  60.  
  61.     Private Function Deserialize(Of T)() As T
  62.         Dim ReturnListOfT = CreateInstance(Of T)()
  63.  
  64.         ' Create a new file stream for reading the XML file
  65.         Using ReadFileStream = New FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read)
  66.             ' Construct a XmlSerializer and use it  
  67.             ' to serialize the data from the stream.
  68.             Dim SerializerObj = New XmlSerializer(GetType(T))
  69.  
  70.             Try
  71.                 ' Deserialize the hashtable from the file
  72.                 ReturnListOfT = DirectCast(SerializerObj.Deserialize(ReadFileStream), T)
  73.             Catch ex As Exception
  74.                 Console.WriteLine(String.Format("Failed to serialize. Reason: {0}", ex.Message))
  75.             End Try
  76.         End Using
  77.         ' return the Deserialized data.
  78.         Return ReturnListOfT
  79.     End Function
  80.  
  81.     ' function to create instance of T
  82.     Private Function CreateInstance(Of T)() As T
  83.         Return DirectCast(Activator.CreateInstance(GetType(T)), T)
  84.     End Function
  85.  
  86. End Module
  87.  
  88. ' The objects we are going to Serializing/De-serializing
  89. <XmlInclude(GetType(Line))>
  90. <XmlInclude(GetType(Circle))>
  91. <XmlInclude(GetType(Square))>
  92. <Serializable()>
  93. Public Class DrawingObject
  94.  
  95.     Public Sub New()
  96.  
  97.     End Sub
  98.  
  99.     Public Overridable Function Draw() As String
  100.         Return "generic drawing object."
  101.     End Function
  102. End Class
  103.  
  104. <Serializable()>
  105. Public Class Square
  106.     Inherits DrawingObject
  107.  
  108.     Public Overrides Function Draw() As String
  109.         Return "Circle."
  110.     End Function
  111.  
  112.     Public Sub New()
  113.     End Sub
  114.  
  115.     Public Sub New(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
  116.         Me.X1 = x1
  117.         Me.Y1 = y1
  118.         Me.X2 = x2
  119.         Me.Y2 = y2
  120.     End Sub
  121.  
  122.     Public Property X1 As Integer
  123.     Public Property Y1 As Integer
  124.  
  125.     Public Property X2 As Integer
  126.     Public Property Y2 As Integer
  127. End Class
  128.  
  129. <Serializable()>
  130. Public Class Line
  131.     Inherits DrawingObject
  132.  
  133.     Public Overrides Function Draw() As String
  134.         Return "Line."
  135.     End Function
  136.  
  137.     Public Sub New()
  138.     End Sub
  139.  
  140.     Public Sub New(x As Integer, y As Integer)
  141.  
  142.         Me.X = x
  143.         Me.Y = y
  144.     End Sub
  145.  
  146.     Public Property X As Integer
  147.     Public Property Y As Integer
  148. End Class
  149.  
  150. <Serializable()>
  151. Public Class Circle
  152.     Inherits DrawingObject
  153.  
  154.     Public Overrides Function Draw() As String
  155.         Return "Circle."
  156.     End Function
  157.  
  158.     Public Sub New()
  159.     End Sub
  160.  
  161.     Public Sub New(radius As Integer, x As Integer, y As Integer)
  162.         Me.Radius = radius
  163.         Me.X = x
  164.         Me.Y = y
  165.     End Sub
  166.  
  167.     Public Property Radius As Integer
  168.     Public Property X As Integer
  169.     Public Property Y As Integer
  170.  
  171. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement