Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Collections
- Imports System.Runtime.Serialization
- ' Note: When building this code, you must reference the
- ' System.Runtime.Serialization.Formatters.Soap.dll assembly.
- Imports System.Runtime.Serialization.Formatters.Soap
- Imports System.Xml.Serialization
- Module Module1
- Sub Main()
- 'Create some objects
- Dim dObj As New Hashtable()
- dObj(0) = New Line(100, 100)
- dObj(1) = New Circle(50, 10, 10)
- dObj(2) = New Square(10, 10, 20, 20)
- dObj(3) = New DrawingObject()
- Serialize(dObj)
- ' deserialize to a generic list
- Dim deserializedList As New Hashtable(Deserialize())
- ' display the results
- For Each DrawingObject As DrawingObject In deserializedList.Values
- Select Case DrawingObject.GetType().Name
- Case "Line"
- Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
- Dim line As Line = DirectCast(DrawingObject, Line)
- Case "Circle"
- Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
- Dim circle As Circle = DirectCast(DrawingObject, Circle)
- Case "Square"
- Dim square As Square = DirectCast(DrawingObject, Square)
- Console.WriteLine(String.Format("I'm a {0}", DrawingObject.Draw()))
- Case Else
- Console.WriteLine(String.Format("I'm just a {0}", DrawingObject.Draw()))
- End Select
- Next
- Console.ReadLine()
- End Sub
- Sub Serialize(data As Hashtable)
- ' Use a file stream here.
- Using fs As New FileStream("Test.soap", FileMode.Create)
- ' Construct a SoapFormatter and use it
- ' to serialize the data to the stream.
- Dim formatter As New SoapFormatter
- Try
- ' Serialize EmployeeList to the file stream
- formatter.Serialize(fs, data)
- Catch e As SerializationException
- Console.WriteLine("Failed to serialize. Reason: " & e.Message)
- End Try
- End Using
- End Sub
- Function Deserialize() As Hashtable
- ' Create an instance of T
- Dim ReturnListOfT1 As New Hashtable()
- ' Open the file containing the data that you want to deserialize.
- Using fs As New FileStream("Test.soap", FileMode.Open)
- ' Construct a SoapFormatter and use it
- ' to serialize the data from the stream.
- Dim formatter As New SoapFormatter
- Try
- fs.Position = 0
- ' Deserialize the hashtable from the file
- ReturnListOfT1 = formatter.Deserialize(fs)
- Catch e As SerializationException
- Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
- End Try
- End Using
- ' return the Deserialized data.
- Return ReturnListOfT1
- End Function
- End Module
- ' The object we are going to Serializing/De-serializing
- <XmlInclude(GetType(Line))>
- <XmlInclude(GetType(Circle))>
- <XmlInclude(GetType(Square))>
- <Serializable()>
- Public Class DrawingObject
- Public Sub New()
- End Sub
- Public Overridable Function Draw() As String
- Return "generic drawing object."
- End Function
- End Class
- <Serializable()>
- Public Class Square
- Inherits DrawingObject
- Public Overrides Function Draw() As String
- Return "Square."
- End Function
- Public Sub New()
- End Sub
- Public Sub New(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
- Me.X1 = x1
- Me.Y1 = y1
- Me.X2 = x2
- Me.Y2 = y2
- End Sub
- Public Property X1 As Integer
- Public Property Y1 As Integer
- Public Property X2 As Integer
- Public Property Y2 As Integer
- End Class
- <Serializable()>
- Public Class Line
- Inherits DrawingObject
- Public Overrides Function Draw() As String
- Return "Line."
- End Function
- Public Sub New()
- End Sub
- Public Sub New(x As Integer, y As Integer)
- Me.X = x
- Me.Y = y
- End Sub
- Public Property X As Integer
- Public Property Y As Integer
- End Class
- <Serializable()>
- Public Class Circle
- Inherits DrawingObject
- Public Overrides Function Draw() As String
- Return "Circle."
- End Function
- Public Sub New()
- End Sub
- Public Sub New(radius As Integer, x As Integer, y As Integer)
- Me.Radius = radius
- Me.X = x
- Me.Y = y
- End Sub
- Public Property Radius As Integer
- Public Property X As Integer
- Public Property Y As Integer
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement