Deserialize byte [] in System.Collections.Hashtable

0

I get in touch to see if there is luck and someone has stuck with this problem before.

I have a byte [] which contains a hashtable serialized, and I want to deserialize it in xamarin forms, that is, deserialize it for android, ios and windows_uwp in order to use its content for some exercises.

Searching I found that the class that could be used to deserialize was "BinaryFormatter", problem, it is not compatible with windows_uwp.

My second option was to use the System.Xml.Serialization.XmlSerializer method, with the following code:

System.IO.MemoryStream mem = new System.IO.MemoryStream(Buffer);
System.Xml.Serialization.XmlSerializer XML = new System.Xml.Serialization.XmlSerializer(Type.GetType("System.Collections.Hashtable"));
lst = (System.Collections.Hashtable)XML.Deserialize(mem);

The problem is that it returns the following exception that I do not know how to solve:

The type System.Collections.Hashtable is not supported because it implements IDictionary.

Let's see if there is luck and someone can throw a cable with this problem

    
asked by Marcos Muñoz Morales 13.02.2018 в 13:55
source

1 answer

0

This generic deserialization method could help you as you say it BinaryFormater

   private static T ByteArrayToObject<T>(byte[] data)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            var binaryFormatter = new BinaryFormatter();
            ms.Write(data, 0, data.Length);
            ms.Seek(0, SeekOrigin.Begin);
            T obj = (T)binaryFormatter.Deserialize(ms);
            return obj;
        }
    }

To use it you can do something like this ...

var hashTableUnserialize = ByteArrayToObject<Hashtable>(dataSerialize);

Create a complete example that can help or guide you

  • SerializeObjectToByteArray.cs > Serialize and Deserialize Object To Byte Array (with use Generics) / Serialize and deserialize object to byte array (using Generics) link

I hope it will help or guide you

    
answered by 14.02.2018 в 16:24