views:

1095

answers:

4

Could somebody tell me how can I convert byte[] to ArrayList by using C# under Windows Mobile?

Later edit:

  1. this would go like having an ArrayList containing instances of a custom type. This list goes to a database (into a blob) as a byte array (the conversion is done by the database API); What I want is to revert the byte[] to ArrayList;

  2. .NET CF does not provide the BinaryFormatter;

+12  A: 

All arrays inherit off ICollection, so you can just use

ArrayList list = new ArrayList(bytearray);

although I would use the generic List<byte> myself using the same method, as that prevents boxing of each byte value in the array. Although arrays don't staticly inherit off the generic IList for the respective type, the CLR adds relevant implementations to each array instance at runtime (see the Important Note here)

thecoop
What did you mean by 'convert byte[] to ArrayList' then?
thecoop
I think @net has a byte array representing a serialized ArrayList instance, and want to deserialize the bytes into an ArrayList instance.
Fredrik Mörk
Yes, that's correct
thelost
Presumably, the method you used to serialize the arraylist has a corresponding deserialize method?
thecoop
I get the byte[] from a database BLOB
thelost
And what object type are you trying to put into the arraylist?
thecoop
a custom one... that contains an enum and some other int, string, DateTime members
thelost
thecoop
Could you please give me an example of how can I convert a byte[] to an ArrayList of Hashtables ?
thelost
+2  A: 

Can't you just do this?

ArrayList list = new ArrayList(byteArray);
Brian Genisio
+2  A: 

ArrayList is untyped, and should only be used for compatibility.

I suggest you use a List<byte>:

var list = new List<byte>(byteArray);

Edit: If the database API does the conversion, shouldn't it provide a way to deserialize? Try using Reflector to find out how it does the conversion.

Yannick M.
A: 

It seems the CF doesn't support the BinaryFormatter. Do you control the component that is sending that binary data? Can't you transform the data to Xml in that component? If not take a look at the Compact Formatter

bruno conde
BinaryFormatter is not available under the CF edition
thelost
I cannot control it. The database API does the conversion.
thelost