C # Convert byte array to generic list

I have a list of my own that I want to embed as a resource so that it can be copied with every new install. However, my list is serialized as a binary, and when I add it as a resource, I cannot just copy it, because C # treats it as a byte array. I need to be able to convert this byte array back to my custom list when I retrieve the file from my resources. Can anyone give me an idea on how to accomplish this conversion?

Thank!

+2


source to share


3 answers


How did you serialize it? Usually you just cancel this process. For example:

BinaryFormatter bf = new BinaryFormatter();
using(Stream ms = new MemoryStream(bytes)) {
    List<Foo> myList = (List<Foo>)bf.Deserialize(ms);
}

      



Obviously, you might need to tweak it if you used a different serializer! Or, if you can get the data like Stream

(and not byte[]

), you can lose step MemoryStream

...

+6


source


How is the list serialized? You must have access to an equivalent method Deserialize()

that can be returned to the original list type.



+1


source


You need to deserialize your byte array into an instance of your list. The way to do this depends on the mechanism by which you serialized it. If you've used a BinaryFormatter

serialize serialize, for example use it to deserialize.

+1


source







All Articles