byte[] to ArrayList?

一世执手 提交于 2019-12-10 17:10:20

问题


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;


回答1:


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)




回答2:


Can't you just do this?

ArrayList list = new ArrayList(byteArray);



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/1433596/byte-to-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!