在Silverlight中将对象(例如,人,员工之类的任何对象)转换为byte [] [英] convert object(i.e any object like person, employee) to byte[] in silverlight

查看:54
本文介绍了在Silverlight中将对象(例如,人,员工之类的任何对象)转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人员对象,需要将其存储为byte [],然后再次检索该byte []并转换为人员对象
,Silverlight中不提供BinaryFormatter

i have a person object and need to store it as byte[] and again retrieve that byte[] and convert to person object and BinaryFormatter is not availabe in silverlight

推荐答案

由于t0mm13b提及的名称空间不是Silverlight .NET引擎的一部分,因此正确的方法是利用此方法来利用数据协定序列化程序:

Because the namespaces mentioned by t0mm13b are not part of the Silverlight .NET engine, the correct way to is to use this workaround leveraging the data contract serializer:

http://forums.silverlight.net/ forums / t / 23161.aspx

来自链接:

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);
}

这篇关于在Silverlight中将对象(例如,人,员工之类的任何对象)转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆