Serializable attribute in silverlight 4

Is it true or not, or do we not have the Serializable attribute in silverlight 4? I have some confusing answers online. When I try to use it in my code, I get a namespace error. These are my included

using System; using System.ComponentModel; using System.Collections.Generic; using System.Runtime.Serialization; 

I have the System, System.Runtime.Serialization assemblies added to my project.

The next question: if it is not available in Silverlight, how to serialize a singleton correctly? Since I planned to use the example here http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx

thanks

+4
source share
1 answer

This is a .NET attribute that cannot be used in Silverlight, but you can use DataContract for serialization.

For autonomous (non-WCF) serialization / deserialization, you can use three components:

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

An example from this topic: http://forums.silverlight.net/forums/p/23161/82135.aspx

+7
source

Source: https://habr.com/ru/post/1344647/


All Articles