ISerializable 를 상속 받아 serializable class 구현하기
Imports System
Imports System.IO
Imports System.Text
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Module Module1
Sub Main()
Dim order As New Order("1002", "My First Order", "Seoul, Korea")
Console.WriteLine(order.ToString)
Dim str As Stream = File.Create("MyOrder.bin")
Dim bf As New BinaryFormatter
' serialize the order object specifying another application domatin
' as the destination of the serialized data. All data including the employee addrsss is serialized
bf.Context = New StreamingContext(StreamingContextStates.CrossAppDomain)
bf.Serialize(str, order)
str.Close()
' deselialize and display the order object
str = File.OpenRead("MyOrder.bin")
bf = New BinaryFormatter
order = DirectCast(bf.Deserialize(str), Order)
str.Close()
Console.WriteLine(order.ToString())
' selialize the order object specifying a file as the destination
' of the selialized data. In this case, the order addrsss is not include in the serialized data
str = File.Create("MyNextOrder.bin")
bf = New BinaryFormatter
bf.Context = New StreamingContext(StreamingContextStates.File)
bf.Serialize(str, order)
str.Close()
str = File.OpenRead("MyNextOrder.bin")
bf = New BinaryFormatter
order = DirectCast(bf.Deserialize(str), Order)
str.Close()
Console.WriteLine(order.ToString)
Console.WriteLine("")
Console.WriteLine("Main method is completed, please Enter")
Console.ReadLine()
End Sub
End Module
--- Order.vb ----
Imports System
Imports System.IO
Imports System.Text
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
<Serializable()> _
Public Class Order
Implements ISerializable
Private m_OrderNumber As String
Private m_PONumber As String
Private m_ShippingAddress As String
Public Sub New(ByVal OrderNumber As String, _
ByVal PONumber As String, _
ByVal ShippingAddress As String)
m_OrderNumber = OrderNumber
m_PONumber = PONumber
m_ShippingAddress = ShippingAddress
End Sub
' when deserialized, it will be executed
Private Sub New(ByVal Info As SerializationInfo, ByVal context As StreamingContext)
m_OrderNumber = Info.GetString("OrderNumber")
m_PONumber = Info.GetString("PONumber")
Try
m_ShippingAddress = Info.GetString("ShippingAddress")
Catch ex As Exception
m_ShippingAddress = Nothing
End Try
End Sub
Public Property OrderNumber() As String
Get
Return m_OrderNumber
End Get
Set(ByVal value As String)
m_OrderNumber = value
End Set
End Property
Public Property PONumber() As String
Get
Return m_PONumber
End Get
Set(ByVal value As String)
m_PONumber = value
End Set
End Property
Public Property ShippingAddress() As String
Get
If m_ShippingAddress Is Nothing Then
m_ShippingAddress = String.Empty
End If
Return m_ShippingAddress
End Get
Set(ByVal value As String)
m_ShippingAddress = value
End Set
End Property
''' <summary>
''' when serialized, it will be invoked
''' </summary>
''' <param name="info"></param>
''' <param name="context"></param>
''' <remarks></remarks>
Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
info.AddValue("OrderNumber", OrderNumber)
info.AddValue("PONumber", PONumber)
If (context.State And StreamingContextStates.File) = 0 Then
info.AddValue("ShippingAddress", ShippingAddress)
End If
End Sub
Public Overrides Function ToString() As String
Dim str As New StringBuilder
str.AppendLine(String.Format("Order Number: {0}", OrderNumber))
str.AppendLine(String.Format("PO Number: {0}", PONumber))
str.AppendLine(String.Format("Shipping Address: {0}", ShippingAddress))
Return str.ToString
End Function
End Class