Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Collections
- Imports System.Runtime.Serialization.Formatters.Binary
- Imports System.Xml.Serialization
- Module Module1
- Sub Main()
- 'Create some objects
- Dim dObj() = New DrawingObject(3) {}
- 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)
- Dim deserializedList = New List(Of DrawingObject)()
- ' deserialize to a generic list
- deserializedList = Deserialize(Of List(Of DrawingObject))()
- ' display the results
- For Each DrawingObject As DrawingObject In deserializedList
- 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
- Private Sub Serialize(Of T)(data As T)
- ' Use a file stream here.
- Using WriteFileStream As New StreamWriter("test.xml")
- ' Construct a SoapFormatter and use it
- ' to serialize the data to the stream.
- Dim SerializerObj As New XmlSerializer(GetType(DrawingObject()))
- Try
- ' Serialize EmployeeList to the file stream
- SerializerObj.Serialize(WriteFileStream, data)
- Catch ex As Exception
- Console.WriteLine(String.Format("Failed to serialize. Reason: {0}", ex.Message))
- End Try
- End Using
- End Sub
- Private Function Deserialize(Of T)() As T
- Dim ReturnListOfT = CreateInstance(Of T)()
- ' Create a new file stream for reading the XML file
- Using ReadFileStream = New FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read)
- ' Construct a XmlSerializer and use it
- ' to serialize the data from the stream.
- Dim SerializerObj = New XmlSerializer(GetType(T))
- Try
- ' Deserialize the hashtable from the file
- ReturnListOfT = DirectCast(SerializerObj.Deserialize(ReadFileStream), T)
- Catch ex As Exception
- Console.WriteLine(String.Format("Failed to serialize. Reason: {0}", ex.Message))
- End Try
- End Using
- ' return the Deserialized data.
- Return ReturnListOfT
- End Function
- ' function to create instance of T
- Private Function CreateInstance(Of T)() As T
- Return DirectCast(Activator.CreateInstance(GetType(T)), T)
- End Function
- End Module
- ' The objects 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 "Circle."
- 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