Introduction
Some days ago, I was writing a program that needed to parse a truetype font. I found that there is no class to fill a structure from a stream. So I wrote one myself.
Using the Code
Basically, if you only want to read/write a structure with the following conditions...
- All data is stored in fields
- All data is primitive type
... it will be very easy, like this:
result = ObjectReader(Of SomeStruct).Read(reader)
ObjectWriter(Of SomeStruct).Write(obj, writer)
Arrays and non-primitive types can also be read/written, but you need to add some attribute to the field. Here is an example:
<System.Runtime.InteropServices.StructLayout_
(Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure TestStruct
Public aInt As Integer
Public bInt64 As Long
<ArraySize(16)> _
Public bArr As Byte()
<ArraySize(4)> _
Public dIArr As Integer()
<ObjectReaderWriterInclude()> _
Public eNested As TestStruct3
Public fNotInclude As TestStruct2
<ObjectReaderWriterInclude()> _
Public gNested2 As TestStruct2
<ArraySize(2), ObjectReaderWriterInclude()> _
Public hNestedArray As TestStruct2()
End Structure
<System.Runtime.InteropServices.StructLayout_
(Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure TestStruct2
Public aB As Byte
<ArraySize(10)> _
Public bBArr As Byte()
End Structure
<System.Runtime.InteropServices.StructLayout_
(Runtime.InteropServices.LayoutKind.Sequential)> _
Public Class TestStruct3
Public aInt As Integer
<ObjectReaderWriterInclude()> _
Public bNested As TestStruct2
End Class
History