Serialize to byte[]

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hi-

I would like to make some of my classes serializable so that I can store
them in a database. I have found some examples showing serializing to a
file, that works fine. I need to serialize to a byte[].

I thought I could just use a MemoryStream and feed it a byte[], but then I
realized I don't know how big to make the byte[]? I'm a newbie and this is
the first time I have ever messed with serialization, but what is the
correct way to get the size of a variable? I looked at
System.Runtime.InteropServices.Marshal.SizeOf() but it complained about not
being able to get any useful size information. The exact exception was:
Type NSTest.MyObject can not be marshaled as an unmanaged structure; no
meaningful size or offset can be computed.

So.... Where do I start. I must be approaching this all worng. Considering
MS has made SOOOO many other things fool proof and easy to do, there must
also be an easy way to serialize to a byte[].

Any help will be greatly appreciated.

Thanks,
Steve
 
I found a way to accomplish what I want. I'm net getting the size, but
Instead getting the Length on a MemoryStream. Works great.
 
Steve said:
I would like to make some of my classes serializable so that I can store
them in a database. I have found some examples showing serializing to a
file, that works fine. I need to serialize to a byte[].

I thought I could just use a MemoryStream and feed it a byte[], but then I
realized I don't know how big to make the byte[]?

You don't need to feed it a byte array to start with. Just construct a
MemoryStream with the parameterless constructor and call ToArray() when
you're finished writing to it.
 
Thanks Jon,

I have it all working now. I hit one weird snag, after calling Serialize()
I noticed that the stream's position was at the end, so a subsequent read
would result in 0
by setting position to 0, then calling Read() everything was fine. That
just seems strange to me, but I guess the idea is tha tyou can Serialize
many objects to the same stream in series.. so mayeb not so weird.

Hmmm..
Either way, thanks for the suggestion/tip.


Jon Skeet said:
Steve said:
I would like to make some of my classes serializable so that I can store
them in a database. I have found some examples showing serializing to a
file, that works fine. I need to serialize to a byte[].

I thought I could just use a MemoryStream and feed it a byte[], but then I
realized I don't know how big to make the byte[]?

You don't need to feed it a byte array to start with. Just construct a
MemoryStream with the parameterless constructor and call ToArray() when
you're finished writing to it.
 
Steve said:
I have it all working now. I hit one weird snag, after calling Serialize()
I noticed that the stream's position was at the end, so a subsequent read
would result in 0
by setting position to 0, then calling Read() everything was fine. That
just seems strange to me, but I guess the idea is tha tyou can Serialize
many objects to the same stream in series.. so mayeb not so weird.

It's not weird at all, no - after calling Serialize() you've basically
just done a lot of writing to the stream. The MemoryStream has no idea
that you've done serialization particularly, and the serialization
mechanism has no particular reason to rewind the stream.
 
Back
Top