Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert object(i.e any object like person, employee) to byte[] in silverlight

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

like image 875
taher chhabrawala Avatar asked Feb 17 '10 13:02

taher chhabrawala


1 Answers

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

From the link:

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);
}
like image 164
Keith Adler Avatar answered Oct 02 '22 18:10

Keith Adler