如何在Windows Phone 8中将任何类型的Object转换为字节数组?

时间:2013-04-04 05:31:20

标签: c# windows-phone-7 windows-phone-8

我想以字节为单位存储数据。但是,因为我是Windows Phone 8的新手;我不知道怎么做。

任何形式的帮助将不胜感激..谢谢

2 个答案:

答案 0 :(得分:2)

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;

答案 1 :(得分: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;

此代码有助于将您的数据恢复到原始状态。我认为这就是你要求的