Click here to Skip to main content
6,629,885 members and growing! (20,605 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Utilities     Intermediate License: The GNU Lesser General Public License

Object Reader and Writer

By SAPikachu

Reads/Writes an object from/to a stream
VB (VB 9.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), WinXP, Vista
Posted:23 Mar 2008
Views:6,393
Bookmarked:6 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.11 Rating: 2.33 out of 5
2 votes, 66.7%
1

2

3

4
1 vote, 33.3%
5

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...

  1. All data is stored in fields
  2. All data is primitive type

... it will be very easy, like this:

'reader can be BinaryReader or Stream
result = ObjectReader(Of SomeStruct).Read(reader)
 
'writer can be BinaryWriter or Stream
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 ' this field will not be read/written
    <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

  • 2008-03-24: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License

About the Author

SAPikachu


Member

Location: China China

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmvpDave Kreskowiak20:06 22 Oct '09  
GeneralTesting PinmemberStevepsi8:06 27 Mar '08  
GeneralRe: Testing PinmemberSAPikachu18:25 1 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by SAPikachu
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project