Serializable attribute in silverlight 4

后端 未结 1 1596
别那么骄傲
别那么骄傲 2021-01-19 07:42

So do we or do we not have a Serializable attribute in silverlight 4? I have some confusing responses on the internet. When I try to use it in my code, i get a namespace err

相关标签:
1条回答
  • 2021-01-19 08:15

    That's a .NET attribute which you can't use in Silverlight, but you can use DataContract to serialize.

    For stand-alone (non-WCF) serialization/deserialization, there are three components which can be used:

    System.Runtime.Serialization.DataContractSerializer (from System.Runtime.Serialization.dll) System.Runtime.Serialization.Json.DataContractJsonSerializer (from System.ServiceModel.Web.dll) System.Xml.Serialization.XmlSerializer (from System.Xml.Serialization.dll)

    A simple example using the DataContractSerializer:

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

    Example from this thread: http://forums.silverlight.net/forums/p/23161/82135.aspx

    0 讨论(0)
提交回复
热议问题