VB.Net by API/System.Runtime.Serialization/IDeserializationCallback
Версия от 16:40, 26 мая 2010; (обсуждение)
Implements IDeserializationCallback
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap
Public Class Tester
Public Shared Sub Main
Dim myClsSerializable As New SerializeObject()
SerializeBinary(myClsSerializable)
Console.WriteLine(FileContent(False))
Dim myFileStream As FileStream
myFileStream = New FileStream("test.dat", FileMode.Open, FileAccess.Read)
Dim myFormatter As New BinaryFormatter()
myClsSerializable = CType(myFormatter.Deserialize(myFileStream), SerializeObject)
End Sub
Private Shared Function FileContent(ByVal blnBinary As Boolean) As String
Dim strContent As String
Dim myStreamReader As StreamReader
Dim myFileStream As FileStream
Dim i As Integer
Try
myFileStream = New FileStream("test.dat", FileMode.Open, FileAccess.Read)
If blnBinary = True Then
For i = 1 To myFileStream.Length
strContent += myFileStream.ReadByte.ToString + " "
Next
Else
myStreamReader = New StreamReader(myFileStream)
strContent = myStreamReader.ReadToEnd
End If
myFileStream.Flush()
myFileStream.Close()
Return strContent
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Function
Private Shared Sub SerializeBinary(ByVal myClsSerializable As SerializeObject)
Dim myFileStream As FileStream = New FileStream("test.dat", FileMode.Create, FileAccess.Write)
Dim myBFormatter As BinaryFormatter = New BinaryFormatter()
myBFormatter.Serialize(myFileStream, myClsSerializable)
myFileStream.Flush()
myFileStream.Close()
End Sub
End Class
<Serializable()> _
Public Class SerializeObject
Implements IDeserializationCallback
Public firstNumber As Integer
Public secondNumber As Integer
<NonSerialized()> Public thirdNumber As Integer
<NonSerialized()> Public forthNumber As Integer
Public Sub New()
firstNumber = 1000
secondNumber = 2000
thirdNumber = 100
forthNumber = 200
End Sub
Public Sub OnDeserialization(ByVal sender As Object) _
Implements IDeserializationCallback.OnDeserialization
Console.WriteLine("OnDeserialization")
thirdNumber = 100
End Sub
End Class