HOWTO: Convert "Type" to a byte array and Back

G

Guest

Hi All,

I am trying to convert an ArrayList (that can contain an array of any type)
to a byte array.
Using the BinaryWriter and BinaryReader is not helpful, for example since
the ArrayList can contain an array of doubles it is necessary to cast the
following:
binWriter.Write((double)dblArrayList[0]);
However since the ArrayList can be of any type it is not possible (or is it)
to perform a cast at run time.

So what is desired is to have a mechanism that will perform conversions of
an ArrayList (of any type) to a byte array and to do the reverse, convert a
byte array back to an array list.
Note that the type is known by saving the type in a struct as a string I.E.:
"System.Double" along with the ArrayList.

Thanks for any help in advance.

Scott
 
M

Mattias Sjögren

Scott,
So what is desired is to have a mechanism that will perform conversions of
an ArrayList (of any type) to a byte array and to do the reverse, convert a
byte array back to an array list.

Have you tried using the framework's serialization support?


Mattias
 
G

Guest

Mattias,

I was able to use the BinaryFormatter class along with the MemoryStream and
BinaryWriter classes to accomplish this task
The only casting necessary was when deserializing the ArrayList class back.

Thanks!
Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top