SerializeObject和DeserializeObject

时间:2012-09-12 17:35:24

标签: wcf windows-phone-7.1

我使用以下代码,但它提供了错误

Service1.svc.cs

 public static byte[] SerializeObject<T>(T obj)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms))
            {
                DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                dcs.WriteObject(writer, obj); writer.Flush();
                return ms.ToArray();
            }
        }
    }

客户端代码Windows phone

public static T DeserializeObject<T>(byte[] xml) 
    { 
        using (MemoryStream memoryStream = new MemoryStream(xml)) 
        {
            using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memoryStream, XmlDictionaryReaderQuotas.Max)) 
            {
                DataContractSerializer dcs = new DataContractSerializer(typeof(T)); 
                return (T)dcs.ReadObject(reader); 
            } 
        } 
    }

我从代码

下面调用 DeserializeObject
   void svc_Get_Conn(object send, GetConnCompletedEventArgs e)
    {  
        CookieContainer con =DeserializeObject<CookieContainer>(e.Result);
    }

这会产生以下错误

Message = "Type 'System.Net.PathList' with data contract name 'PathList:http://schemas.datacontract.org/2004/07/System.Net' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using...

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

CookieContainer无法序列化。查看this解决方法

欢呼声