在silverlight中将对象(即任何对象,如person、employee)转换为字节[]
我有一个person对象,需要将其存储为byte[],然后再次检索该byte[]并转换为person对象在silverlight中将对象(即任何对象,如person、employee)转换为字节[],silverlight,object,byte,Silverlight,Object,Byte,我有一个person对象,需要将其存储为byte[],然后再次检索该byte[]并转换为person对象 而BinaryFormatter在silverlight中不可用使用序列化类通过MemoryStream将对象转换为字节 using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; .... byte[] bPersonInfo = null; using (Memory
而BinaryFormatter在silverlight中不可用使用序列化类通过MemoryStream将对象转换为字节 using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; .... byte[] bPersonInfo = null; using (MemoryStream mStream = new MemoryStream()) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(mStream, personInfo); bPersonInfo = mStream.ToArray(); } .... // Do what you have to do with bPersonInfo which is a byte Array... // To Convert it back PersonInfo pInfo = null; using (MemoryStream mStream = new MemoryStream(bPersonInfo)){ System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new BinaryFormatter(); pInfo = (PersonInfo)bf.DeSerialize(mStream); } // Now pInfo is a PersonInfo object. 使用System.Runtime.Serialization.Formatters.Binary; 使用System.Runtime.Serialization; .... 字节[]bPersonInfo=null; 使用(MemoryStream mStream=new MemoryStream()) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=new BinaryFormatter(); bf.序列化(mStream、personInfo); bPersonInfo=mStream.ToArray(); } .... //使用bPersonInfo(一个字节数组)执行您必须执行的操作。。。 //把它换回来 PersonInfo pInfo=null; 使用(MemoryStream mStream=新的MemoryStream(bPersonInfo)){ System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=new BinaryFormatter(); pInfo=(PersonInfo)bf.反序列化(mStream); } //现在pInfo是PersonInfo对象。 希望这有帮助, 顺致敬意,
Tom。我使用XML序列化程序将对象转换为字符串,并在Silverlight中成功地将字符串转换为字节[]
object address = new Address();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Address));
StringBuilder stringBuilder = new StringBuilder();
using (StringWriter writer = new StringWriter(stringBuilder))
{
serializer.Serialize(writer, address);
}
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] data = encoding.GetBytes(stringBuilder.ToString());
因为t0mm13b提到的名称空间不是Silverlight.NET引擎的一部分,正确的方法是利用数据协定序列化程序使用此解决方案: 从链接:
string SerializeWithDCS(object obj)
{
if (obj == null) throw new ArgumentNullException("obj");
DataContractSerializer dcs = new DataContractSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
dcs.WriteObject(ms, obj);
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
}
查看自定义二进制序列化和压缩
而且如果你真的需要二进制文件,并且希望它非常快和非常小,那么你应该使用谷歌的protobuf 看看这些性能数据。Protobuf绝对是最快和最小的
我已经成功地将其用于WCF Silverlight,并且会毫不犹豫地再次将其用于新项目。谢谢Tom,Silverlight中没有BinaryFormatter。我不确定为什么会出现upvotes-Silverlight中似乎没有提供Formatters命名空间。还是我遗漏了什么?我在将现有winforms应用程序转换为silverlight时遇到了同样的问题。包含非法函数AllocHGlobal和FreeHGlobal的现有函数为:public static byte[]RawSerialize(object anything){int rawsize=Marshal.SizeOf(anything);IntPtr buffer=Marshal.AllocHGlobal(rawsize);Marshal.StructureToPtr(anything,buffer,false);byte[]rawdatas=新字节[rawsize];Marshal.Copy(缓冲区,rawdatas,0,rawsize);Marshal.FreeHGlobal(缓冲区);返回rawdatas;}