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.Runtime.Serialization
- Module Module1
- Sub Main()
- 'Create some objects
- Dim dObj As New List(Of DrawingObject)()
- dObj.Add(New Line(100, 100))
- dObj.Add(New Circle(50, 10, 10))
- dObj.Add(New Square(10, 10, 20, 20))
- dObj.Add(New DrawingObject())
- For Each drawObj As DrawingObject In dObj
- Select Case drawObj.GetType().Name
- Case "Line"
- Console.WriteLine(String.Format("I'm a {0}", drawObj.Draw()))
- Dim line As Line = DirectCast(drawObj, Line)
- Case "Circle"
- Console.WriteLine(String.Format("I'm a {0}", drawObj.Draw()))
- Dim circle As Circle = DirectCast(drawObj, Circle)
- Case "Square"
- Dim square As Square = DirectCast(drawObj, Square)
- Console.WriteLine(String.Format("I'm a {0}", drawObj.Draw()))
- Case Else
- Console.WriteLine(String.Format("I'm just a {0}", drawObj.Draw()))
- End Select
- drawObj.Draw()
- Next
- ' Serialize
- Serialize(dObj)
- Dim deserializedList = New List(Of DrawingObject)()
- ' deserialize to a generic list
- deserializedList = Deserialize(Of List(Of DrawingObject))()
- 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
- End Sub
- Public Sub Serialize(Of T)(data As T)
- Try
- Using stream As New FileStream("data.bin", FileMode.Create)
- ' create BinaryFormatter
- Dim bin = New Formatters.Binary.BinaryFormatter()
- ' serialize the collection (EmployeeList1) to file (stream)
- bin.Serialize(stream, data)
- End Using
- Catch ex As Exception
- End Try
- End Sub
- Private Function Deserialize(Of T)() As T
- ' Create an instance of T
- Dim ReturnListOfT = CreateInstance(Of T)()
- Try
- Using stream As New FileStream("data.bin", FileMode.Open)
- ' create BinaryFormatter
- Dim bin = New Formatters.Binary.BinaryFormatter()
- ' deserialize the collection (Employee) from file (stream)
- ReturnListOfT = DirectCast(bin.Deserialize(stream), T)
- End Using
- Catch ex As Exception
- End Try
- Return DirectCast(ReturnListOfT, T)
- 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
- <KnownType(GetType(Line))>
- <KnownType(GetType(Circle))>
- <KnownType(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