VB.Net Tutorial/Stream File/Binary Serialization

Материал из VB Эксперт
Перейти к: навигация, поиск

Binary serialization and deserialization for a derived Class

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 ClsInherits()
        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), ClsSerializable)
        Console.WriteLine(myClsSerializable.strMessage)
    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 ClsSerializable)
        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 ClsInherits
    Inherits ClsSerializable
    Public intInNumber As Integer
    Public strInMessage As String
    Public Sub New()
        intInNumber = 1024
    End Sub
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
        intInNumber = CInt(info.GetValue("n", intInNumber.GetType))
    End Sub
    Public Overrides Sub GetObjectData _
     (ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.GetObjectData(info, context)
        info.AddValue("n", intInNumber)
    End Sub

End Class

<Serializable()> _
 Public Class ClsSerializable
              
    Implements ISerializable
    Public intNumber As Integer = 1000
    Public strMessage As String = "this is a string"
    Public lngTest As Long
    Public intArrayX(10) As Integer
    Public intArrayY(10) As Integer
    Public Sub New()
        ChangeMmberValue()
    End Sub
    Public Overridable Sub GetObjectData _
     (ByVal info As SerializationInfo, ByVal context As StreamingContext) _
     Implements ISerializable.GetObjectData
        info.AddValue("intNumber", intNumber)
        info.AddValue("message", strMessage)
        info.AddValue("intArrayX", intArrayX)
    End Sub
    Public Sub New _
     (ByVal info As SerializationInfo, ByVal context As StreamingContext)
        intNumber = CInt(info.GetValue("intNumber", intNumber.GetType))
        strMessage = CStr(info.GetValue("message", strMessage.GetType))
        intArrayX = info.GetValue("intArrayX", intArrayX.GetType)
    End Sub
    Public Sub ChangeMmberValue()
        Dim i As Integer
        For i = 0 To 9
            intArrayX(i) = i * 100
        Next i
        intNumber = 2000
        strMessage = "this is another string"
    End Sub
End Class

Binary serialization for a Class with ISerializable implementation

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 ClsSerializable()
        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), ClsSerializable)
        Console.WriteLine(myClsSerializable.strMessage)
                
    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 ClsSerializable)
        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 ClsSerializable
              
    Implements ISerializable
    Public intNumber As Integer = 1000
    Public strMessage As String = "this is a string"
    Public lngTest As Long
    Public intArrayX(10) As Integer
    Public intArrayY(10) As Integer
    Public Sub New()
        ChangeMmberValue()
    End Sub
    Public Sub GetObjectData _
     (ByVal info As SerializationInfo, ByVal context As StreamingContext) _
     Implements ISerializable.GetObjectData
        info.AddValue("intNumber", intNumber)
        info.AddValue("message", strMessage)
        info.AddValue("intArrayX", intArrayX)
    End Sub
    Public Sub New _
     (ByVal info As SerializationInfo, ByVal context As StreamingContext)
        intNumber = CInt(info.GetValue("intNumber", intNumber.GetType))
        strMessage = CStr(info.GetValue("message", strMessage.GetType))
        intArrayX = info.GetValue("intArrayX", intArrayX.GetType)
    End Sub
    Public Sub ChangeMmberValue()
        Dim i As Integer
        For i = 0 To 9
            intArrayX(i) = i * 100
        Next i
        intNumber = 2000
        strMessage = "this is another string"
    End Sub
End Class

Class binary deserialization

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 ClsSerializable()
        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), ClsSerializable)
        Console.WriteLine(myClsSerializable.intNumber.ToString)
        Console.WriteLine(myClsSerializable.lngNumber.ToString)
        Console.WriteLine(myClsSerializable.strDemo)
                
    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 ClsSerializable)
        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 ClsSerializable
    Public intNumber As Integer = 254
    Public strDemo As String = "This is a poublic  test string"
    Private strpDemo As String = "This is a private  test string"
    <NonSerialized()> Public lngNumber As Long = 123456

 
End Class

Class binary Serialization

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 ClsSerializable()
        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), ClsSerializable)
        Console.WriteLine(myClsSerializable.intNumber.ToString)
        Console.WriteLine(myClsSerializable.lngNumber.ToString)
        Console.WriteLine(myClsSerializable.strDemo)
                
    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 ClsSerializable)
        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 ClsSerializable
    Public intNumber As Integer = 254
    Public strDemo As String = "This is a poublic  test string"
    Private strpDemo As String = "This is a private  test string"
    <NonSerialized()> Public lngNumber As Long = 123456

 
End Class