views:

419

answers:

2

Could someone please give me an example on how can I convert a byte[] to an ArrayList of Hashtables with C# ? (the byte[] represents the ArrayList of Hashtables that was previously serialized)

Note: I'm running under Windows Mobile, which does not provide BinaryFormatter.

+1  A: 

When you say "that was previously serialized" do you mean "serialized with BinaryFormatter"? If so, no - you'd need BinaryFormatter to deserialize too. If the Compact Framework doesn't support that, you're basically out of luck. I mean, if the serialization format is documented somewhere (I'm not sure whether it is or not) you could write your own BinaryFormatter - but it would probably be somewhat tricky.

Instead, you should choose a serialization format which is supported everywhere you need to serialize/deserialize.

Jon Skeet
No... I get the byte[] from a database, by selecting from a blob column. Also, my ArrayList is automatically converted by the database API into the byte[], so I do not have access to this process
thelost
But what stored the byte array to start with? You'll either need to change that to use a more sensible serialization format, or possibly put an intermediate stage in on a server. How are you getting the data to the mobile device to start with? If it's via a web service, that web service could deserialize from what the database gives, and reserialize in a format that you'll be able to use from Windows Mobile.
Jon Skeet
I'm using SQLite. When I pass an object to be stored into a blob column, it automatically converts it to byte[]. The fact is I doesn't seem to be possible to change the serializer to a custom implementation and a binary deserializer is not provided by default by .NET CF
thelost
+2  A: 

Also, my ArrayList is automatically converted by the database API into the byte[]

Frankly, you are going to have to find the actual serialization API used to stand a chance of reconstructing this data. It is probably BinaryFormatter, which is notoriously non-portable and version-intolerant.

If you need to store data as binary, and use it long-term (database) and between platforms (CF), then you are going to have to use a suitable serializer. For example, protobuf-net would work in principal (although it won't like untyped ArrayList, preferring typed List<T> etc).

If the data is currently stored as BinaryFormatter, then your best bet would be to extract it using your current system and re-package it (perhaps in a different column or table) in a more suitable serialization format.

Marc Gravell
I am currently using SQLite. I did not find yet any mean of controlling the way objects get serialized, so I am not sure using a different serialization / deserialization tool would be possible. Do you have any experience on this ?
thelost
Not directly with SQLite; but could you give it a pre-processed `byte[]`?
Marc Gravell
I tryed the following:List<Dummy> myList = new List<Dummy>();myList.add(new Dummy());Stream destination = new MemoryStream();ProtoBuf.Serializer.Serialize(destination, myList);and I received TypeLoadException: "Only data-contract classes (and lists/arrays of such) can be processed (error processing List`1)".I read some explanations at http://code.google.com/p/protobuf-net/issues/detail?id=58 but cannot cope it yet. Any thoughts ?
thelost
protobuf-net needs to be convinced that it understands what you want. You could add `[ProtoContract]` to the type, and `[ProtoMember(1)]`, `[ProtoMember(2)]`, etc to the properties, for example. There are other ways of doing this, but that is the simplest to explain in a comment!
Marc Gravell
Thank a lot! Everything worked like a charm !!! They should offer some documentation/example @ protobuf-net, though. Again, thank you !
thelost
"they" would be "me" ;-p
Marc Gravell