Advertisement
NAK

Polymorphism/Serialization BinaryFormatter(VB.NET)

NAK
Dec 2nd, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.18 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Collections
  3. Imports System.Runtime.Serialization.Formatters.Binary
  4. Imports System.Runtime.Serialization
  5.  
  6. Module Module1
  7.  
  8.     Sub Main()
  9.         'Create some objects
  10.         Dim dObj As New List(Of DrawingObject)()
  11.  
  12.         dObj.Add(New Line(100, 100))
  13.         dObj.Add(New Circle(50, 10, 10))
  14.         dObj.Add(New Square(10, 10, 20, 20))
  15.         dObj.Add(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.bin", FileMode.Create)
  64.  
  65.                 ' create BinaryFormatter
  66.                 Dim bin = New Formatters.Binary.BinaryFormatter()
  67.                 ' serialize the collection (EmployeeList1) to file (stream)
  68.                 bin.Serialize(stream, data)
  69.  
  70.             End Using
  71.         Catch ex As Exception
  72.         End Try
  73.     End Sub
  74.  
  75.     Private Function Deserialize(Of T)() As T
  76.         ' Create an instance of T
  77.         Dim ReturnListOfT = CreateInstance(Of T)()
  78.  
  79.         Try
  80.             Using stream As New FileStream("data.bin", FileMode.Open)
  81.  
  82.                 ' create BinaryFormatter
  83.                 Dim bin = New Formatters.Binary.BinaryFormatter()
  84.                 ' deserialize the collection (Employee) from file (stream)
  85.                 ReturnListOfT = DirectCast(bin.Deserialize(stream), T)
  86.  
  87.             End Using
  88.         Catch ex As Exception
  89.         End Try
  90.  
  91.         Return DirectCast(ReturnListOfT, T)
  92.     End Function
  93.  
  94.     ' function to create instance of T
  95.     Private Function CreateInstance(Of T)() As T
  96.         Return DirectCast(Activator.CreateInstance(GetType(T)), T)
  97.     End Function
  98.  
  99. End Module
  100.  
  101. <KnownType(GetType(Line))>
  102. <KnownType(GetType(Circle))>
  103. <KnownType(GetType(Square))>
  104. <Serializable()>
  105. Public Class DrawingObject
  106.  
  107.     Public Sub New()
  108.  
  109.     End Sub
  110.  
  111.     Public Overridable Function Draw() As String
  112.         Return "generic drawing object."
  113.     End Function
  114. End Class
  115.  
  116. <Serializable()>
  117. Public Class Square
  118.     Inherits DrawingObject
  119.  
  120.     Public Overrides Function Draw() As String
  121.         Return "Circle."
  122.     End Function
  123.  
  124.     Public Sub New()
  125.     End Sub
  126.  
  127.     Public Sub New(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
  128.         Me.X1 = x1
  129.         Me.Y1 = y1
  130.         Me.X2 = x2
  131.         Me.Y2 = y2
  132.     End Sub
  133.  
  134.     Public Property X1 As Integer
  135.     Public Property Y1 As Integer
  136.     Public Property X2 As Integer
  137.     Public Property Y2 As Integer
  138. End Class
  139.  
  140. <Serializable()>
  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.     Public Property X As Integer
  158.     Public Property Y As Integer
  159. End Class
  160.  
  161. <Serializable()>
  162. Public Class Circle
  163.     Inherits DrawingObject
  164.  
  165.     Public Overrides Function Draw() As String
  166.         Return "Circle."
  167.     End Function
  168.  
  169.     Public Sub New()
  170.     End Sub
  171.  
  172.     Public Sub New(radius As Integer, x As Integer, y As Integer)
  173.         Me.Radius = radius
  174.         Me.X = x
  175.         Me.Y = y
  176.     End Sub
  177.  
  178.     Public Property Radius As Integer
  179.     Public Property X As Integer
  180.     Public Property Y As Integer
  181.  
  182. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement