Advertisement
NAK

Polymorphism/Serialization SoapFormatter (VB.NET)

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