Advertisement
NAK

Polymorphism/Serialization DataContractSerializer (VB.NET)

NAK
Dec 2nd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.22 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Runtime.Serialization
  3. Imports System.Collections.Generic
  4. Imports System.ComponentModel
  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.         For Each drawObj As DrawingObject In dObj
  18.  
  19.             Select Case drawObj.GetType().Name
  20.                 Case "Line"
  21.                     Console.WriteLine(String.Format("I'm a {0}", drawObj.Draw()))
  22.                     Dim line As Line = DirectCast(drawObj, Line)
  23.                 Case "Circle"
  24.                     Console.WriteLine(String.Format("I'm a {0}", drawObj.Draw()))
  25.                     Dim circle As Circle = DirectCast(drawObj, Circle)
  26.                 Case "Square"
  27.                     Dim square As Square = DirectCast(drawObj, Square)
  28.                     Console.WriteLine(String.Format("I'm a {0}", drawObj.Draw()))
  29.                 Case Else
  30.                     Console.WriteLine(String.Format("I'm just a {0}", drawObj.Draw()))
  31.             End Select
  32.             drawObj.Draw()
  33.         Next
  34.  
  35.         ' Serialize
  36.         Serialize(dObj)
  37.  
  38.         Dim deserializedList = New List(Of DrawingObject)()
  39.         ' deserialize to a generic list
  40.         deserializedList = Deserialize(Of List(Of DrawingObject))()
  41.  
  42.         For Each DrawingObject As DrawingObject In deserializedList
  43.  
  44.             Select Case DrawingObject.GetType().Name
  45.                 Case "Line"
  46.                     Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
  47.                     Dim line As Line = DirectCast(DrawingObject, Line)
  48.                 Case "Circle"
  49.                     Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
  50.                     Dim circle As Circle = DirectCast(DrawingObject, Circle)
  51.                 Case "Square"
  52.                     Dim square As Square = DirectCast(DrawingObject, Square)
  53.                     Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
  54.                 Case Else
  55.                     Console.WriteLine(String.Format("I'm just a {0}", DrawingObject.Draw()))
  56.             End Select
  57.         Next
  58.  
  59.     End Sub
  60.  
  61.     Public Sub Serialize(Of T)(data As T)
  62.         Try
  63.             Using stream As New FileStream("data.xml", FileMode.Create)
  64.                 Dim serializer = New DataContractSerializer(GetType(T))
  65.  
  66.                 serializer.WriteObject(stream, Data)
  67.             End Using
  68.         Catch ex As Exception
  69.         End Try
  70.     End Sub
  71.  
  72.     Private Function Deserialize(Of T)() As T
  73.         ' Create an instance of T
  74.         Dim ReturnListOfT = CreateInstance(Of T)()
  75.  
  76.         Try
  77.             Using stream As New FileStream("data.xml", FileMode.Open)
  78.                 ' create DataContractSerializer
  79.                 Dim serializer = New DataContractSerializer(GetType(T))
  80.                 ' deserialize the collection (Employee) from file (stream)
  81.                 ReturnListOfT = DirectCast(serializer.ReadObject(stream), T)
  82.             End Using
  83.         Catch ex As Exception
  84.         End Try
  85.  
  86.         Return DirectCast(ReturnListOfT, T)
  87.     End Function
  88.  
  89.     ' function to create instance of T
  90.     Private Function CreateInstance(Of T)() As T
  91.         Return DirectCast(Activator.CreateInstance(GetType(T)), T)
  92.     End Function
  93.  
  94. End Module
  95.  
  96. <KnownType(GetType(Line))>
  97. <KnownType(GetType(Circle))>
  98. <KnownType(GetType(Square))>
  99. <DataContract>
  100. Public Class DrawingObject
  101.  
  102.     Public Sub New()
  103.  
  104.     End Sub
  105.  
  106.     Public Overridable Function Draw() As String
  107.         Return "generic drawing object."
  108.     End Function
  109. End Class
  110.  
  111. <DataContract>
  112. Public Class Square
  113.     Inherits DrawingObject
  114.  
  115.     Public Overrides Function Draw() As String
  116.         Return "Circle."
  117.     End Function
  118.  
  119.     Public Sub New()
  120.     End Sub
  121.  
  122.     Public Sub New(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
  123.         Me.X1 = x1
  124.         Me.Y1 = y1
  125.         Me.X2 = x2
  126.         Me.Y2 = y2
  127.     End Sub
  128.  
  129.     <DataMember>
  130.     Public Property X1 As Integer
  131.     <DataMember>
  132.     Public Property Y1 As Integer
  133.  
  134.     <DataMember>
  135.     Public Property X2 As Integer
  136.     <DataMember>
  137.     Public Property Y2 As Integer
  138. End Class
  139.  
  140. <DataContract>
  141. Public Class Line
  142.     Inherits DrawingObject
  143.  
  144.     Public Overrides Function Draw() As String
  145.         Return "Line."
  146.     End Function
  147.  
  148.     Public Sub New()
  149.     End Sub
  150.  
  151.     Public Sub New(x As Integer, y As Integer)
  152.  
  153.         Me.X = x
  154.         Me.Y = y
  155.     End Sub
  156.  
  157.     <DataMember>
  158.     Public Property X As Integer
  159.     <DataMember>
  160.     Public Property Y As Integer
  161. End Class
  162.  
  163. <DataContract>
  164. Public Class Circle
  165.     Inherits DrawingObject
  166.  
  167.     Public Overrides Function Draw() As String
  168.         Return "Circle."
  169.     End Function
  170.  
  171.     Public Sub New()
  172.     End Sub
  173.  
  174.     Public Sub New(radius As Integer, x As Integer, y As Integer)
  175.         Me.Radius = radius
  176.         Me.X = x
  177.         Me.Y = y
  178.     End Sub
  179.  
  180.     <DataMember>
  181.     Public Property Radius As Integer
  182.     <DataMember>
  183.     Public Property X As Integer
  184.     <DataMember>
  185.     Public Property Y As Integer
  186.  
  187. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement