How to convert any type of Object into byte array

2019-09-21 19:55发布

问题:

I want to store data in bytes. But as I'm new to Windows Phone 8; I don't know how to do it.

Any kind of help will be appreciated.. thanks

回答1:

DataContractSerializer serializer = new    DataContractSerializer(typeof(List<Dictionary<String, String>>));

byte[] byteArr;

using (var ms = new System.IO.MemoryStream())
{
 serializer.WriteObject(ms, stringlist);
 byteArr = ms.ToArray();
}
 return byteArr;


回答2:

System.Runtime.Serialization.DataContractSerializer serializer = new     System.Runtime.Serialization.DataContractSerializer(typeof(List<Dictionary<String, String>>));
List<Dictionary<String, String>> stringlist;
using (var ms = new System.IO.MemoryStream(byteArr))
{
stringlist = (List<Dictionary<String, String>>)serializer.ReadObject(ms);
}
return stringlist;

This code is helpful to get your data back to original state. I think this is what you are asking for