Sure. Let's look at a simple example (this is totally in this email editor,
so intellisnese, etc. are missing and I might have erros, but you'll get the
idea).
Let's assume we have a struct Foo with 2 DWORD members, X and Y.
sequentially they would be an 8-byte array with X followed by Y. The
following class has one copy of the data, properties that access those
bytes, a constructor and an operator that allows you to create a Foo from a
byte array (pulled from from your stream read) and an operator to get a
Foo's byte array (send it to your stream write).
public class Foo
{
private byte[] m_bytes = new byte[8];
// ctors
public Foo(){}
public Foo(byte[] bytes) { Buffer.BlockCopy(m_bytes, bytes, 0); }
// properties
public uint X
{
get{ return BitConverter.ToUInt32(m_bytes[0]); }
set{ Buffer.BlockCopy(m_bytes, BitConverter.GetBits(value), 0); }
}
public uint Y
{
get{ return BitConverter.ToUInt32(m_bytes[4]); }
set{ Buffer.BlockCopy(m_bytes, BitConverter.GetBits(value), 4); }
}
// operators
public static implicit operator byte[](Foo f)
{
return f.m_bytes;
}
public static implicit operator Foo(byte[] bytes)
{
return new Foo(bytes);
}
}
Does that make sense?
-Chris
"Stan" <(E-Mail Removed)> wrote in message
news:068f01c3877f$54fe63a0$(E-Mail Removed)...
> hmm.. perhaps I am looking at the wrong implementation,
> but the one I see simply parses some string and tries to
> fill in the parameters..
>
> What I would like to do is go from file directly to
> structure (avoiding as much intermediate buffers as
> necessary). For instance, I am using FileStream in
> the .NET CF. It allows me to read from file and place the
> information into a Byte array.
>
> Let's say I have an array of SYSTEMTIME structures (say
> 16 bytes each) that I'd like to fill in. I know the file
> I'm reading has the exact/correct layout and has 5
> SYSTEMTIME structures. Since I see that FileStream only
> provides me with functions to read into a byte array, I
> create a byte array of length (5*16) and read in all the
> data. Is there a way (as John said) to "cast" this byte
> array as an array of SYSTEMTIME structures? Or is there
> an even better way? Right now it just seems like I have
> to create yet another array with the explicit
> (SYSTEMTIME) type and manually parse the Byte array, not
> only taking time but potentially twice the memory...
> Thanks.
>
>
> >-----Original Message-----
> >Take a look at the SYSTEMTIME class implementation in
> OpenNETCF.WinAPI - it
> >(and several other classes) does exactly what you want.
> >
> >--
> >Chris Tacke, eMVP
> >Advisory Board Member
> >www.OpenNETCF.org
> >---
> >Windows CE Product Manager
> >Applied Data Systems
> >www.applieddata.net
> >
> >"Stan" <(E-Mail Removed)> wrote in message
> >news:102001c386ea$56712230$(E-Mail Removed)...
> >> Hi,
> >>
> >> In my file I store data of known structure types.
> >> Currently I am reading from file, storing into a byte
> >> array, and then do a manual conversion from array to
> >> structure by parsing all the bytes. Is there an easy
> way
> >> to just tell C# that some byte array is actually of
> >> structure MyStruct? It just seems like a waste since I
> >> end up w/ two copies of my structure: the final product
> >> and the byte array... Thanks.
> >>
> >> + Stan +
> >
> >
> >.
> >