首页 新闻 会员 周边 捐助

在Silverlight类库中如何使用[Serializable]?

-1
悬赏园豆:5 [已关闭问题]

还有ArrayList,ICloneable接口

需要using什么,试了再回答。

 

风海迷沙的主页 风海迷沙 | 老鸟四级 | 园豆:4453
提问于:2009-11-18 14:52
< >
分享
其他回答(1)
0

SL没有Serializable标签

Kolor | 园豆:55 (初学一级) | 2009-11-24 14:18
0

不能用,,,只能自己写一个

 public static class SerializeHelper
    {
        public static byte[] Serialize(this object obj)
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());

            MemoryStream ms = new MemoryStream();

            serializer.WriteObject(ms, obj);

            ms.Seek(0, 0);
            return ms.ToArray();
        }

        public static T Deserialize<T>(this byte[] data)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof (T));

            MemoryStream ms = new MemoryStream(data);
            ms.Seek(0, 0);
            return (T) (serializer.ReadObject(ms));
        }

        public static string SerializeToString(this object obj)
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());

            MemoryStream ms = new MemoryStream();

            serializer.WriteObject(ms, obj);

            return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int) ms.Length);
        }

        public static T DeserializeFromString<T>(this string data)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(data);

            DataContractSerializer serializer = new DataContractSerializer(typeof (T));

            MemoryStream ms = new MemoryStream(buffer);
            ms.Seek(0, 0);
            return (T) (serializer.ReadObject(ms));
        }
    }
★火星人★ | 园豆:165 (初学一级) | 2012-04-10 11:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册