Als Alternativ kann man OOP Prinzipien anwenden und ganz schnell und effizient dafür sorgen, dass alle Objekte sich um die eigene Serialisierung sorgen, ganz ohne Interop, Reflection, Tricks mit unmanaged Speicher usw.
Public Interface ISaveable
Sub Save(ByVal bw As IO.BinaryWriter)
Sub Restore(ByVal br As IO.BinaryReader)
End Interface
Public Structure s1
Implements ISaveable
Public i1 As Integer
Public d1 As Double
Public l1a() As Long
Public s1 As String
Public Sub New(ByVal i1 As Integer, ByVal d1 As Double, ByVal l1a() As _
Long, ByVal s1 As String)
Me.i1 = i1
Me.d1 = d1
Me.l1a = l1a
Me.s1 = s1
End Sub
Public Overrides Function tostring() As String
Dim l1as As String = " {"
For Each l As Long In l1a
If l1as <> " {" Then l1as += ", "
l1as += l.ToString()
Next
l1as += "} "
Return i1.ToString() & " " & d1.ToString() & l1as & ">" & s1 & "<"
End Function
Public Sub Restore(ByVal br As System.IO.BinaryReader) Implements _
ISaveable.Restore
i1 = br.ReadInt32()
d1 = br.ReadDouble()
Dim cnt As Integer = br.ReadInt32()
ReDim l1a(cnt - 1)
For i As Integer = 0 To cnt - 1
l1a(i) = br.ReadInt64()
Next
cnt = br.ReadInt32()
Dim sb() As Byte = br.ReadBytes(cnt)
s1 = System.Text.Encoding.UTF8.GetString(sb)
End Sub
Public Sub Save(ByVal bw As System.IO.BinaryWriter) Implements _
ISaveable.Save
bw.Write(i1)
bw.Write(d1)
bw.Write(l1a.Length)
For Each l As Long In l1a
bw.Write(l)
Next
Dim sb() As Byte = System.Text.Encoding.UTF8.GetBytes(s1)
bw.Write(sb.Length)
bw.Write(sb, 0, sb.Length)
End Sub
End Structure
Public Structure data
Implements ISaveable
Public data() As s1
Public Overrides Function ToString() As String
Dim res As String = "||"
For Each s1i As s1 In data
If res <> "||" Then res += "||"
res += s1i.tostring()
Next
res += "||"
Return res
End Function
Public Sub Restore(ByVal br As System.IO.BinaryReader) Implements _
ISaveable.Restore
Dim cnt As Integer = br.ReadInt32()
ReDim data(cnt - 1)
For i As Integer = 0 To cnt - 1
data(i) = New s1
data(i).Restore(br)
Next
End Sub
Public Sub Save(ByVal bw As System.IO.BinaryWriter) Implements _
ISaveable.Save
bw.Write(data.Length)
For Each s1i As s1 In data
s1i.Save(bw)
Next
End Sub
End Structure
Dim data1 As New data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
ReDim data1.data(1)
data1.data(0) = New s1(2, 3.4, New Long() {55, 66}, "string1")
data1.data(1) = New s1(21, 32.43, New Long() {556, 667}, "string2")
Debug.WriteLine(data1.ToString())
Dim fso As New IO.FileStream("C:\test.dat", IO.FileMode.Create, _
IO.FileAccess.Write, IO.FileShare.None)
Dim bw As New IO.BinaryWriter(fso)
bw.Write(&H1020304) ' Id
bw.Write(1) ' Version
data1.Save(bw)
bw.Close()
data1 = New data
Dim fsi As New IO.FileStream("C:\test.dat", IO.FileMode.Open, _
IO.FileAccess.Read, IO.FileShare.Read)
Dim br As New IO.BinaryReader(fsi)
Dim id As Integer = br.ReadInt32() ' Id
' (prüfen)
Dim vers As Integer = br.ReadInt32() ' Version
' (prüfen)
data1.Restore(br)
br.Close()
Debug.WriteLine(data1.ToString())
End Sub Those people who think they know everything are a great annoyance to those of us who do - Isaac Asimov
Beitrag wurde zuletzt am 28.05.08 um 16:32:54 editiert. |