473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Serialize to byte[]

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
Nov 15 '05 #1
4 11976
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" <st***@nowhere.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #2
Steve <st***@nowhere.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steve <st***@nowhere.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Steve <st***@nowhere.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Carl Gilbert | last post by:
Hi I am trying to serialize an assembly into a byte array. I am using a BinaryFormatter to serialize the assembly into a stream. Once in the stream, would then save this to the database in a...
5
by: Brian Reed | last post by:
I have a class that I want to serialize to an XML string. I want the XML to serialize to utf-8 encoding. When I serialize to an XML file, the data looks great. When I try to serialize to a String...
5
by: Brad | last post by:
I would like to serialize an arraylist of objects to xml so I can store the xml in a database column. How would I code the serializing and deserializing? Below is a (overly) simple, incomplete...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
6
by: Val | last post by:
How can I serialize/deserialize an object into a string. Existing examples seem to be showing this operation for files only. Thansk
3
by: Andrew Robinson | last post by:
I am using the folllowing code to serialize a object to a byte array (byte). It works fine, but my resulting array is size is being rounded up to the next largest 32768 boundary. How can I...
2
by: Leon_Amirreza | last post by:
How can I serialize a uint Type into a Byte?
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an...
4
by: djidan | last post by:
hi, i am a newbe to c#, i'm trying to send en custom object from a client to a server, after reading a lot on the web ifound that i need to use: BinaryFormatter, MemoryStream, and Serialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.