none
Deserialization of Timestamped<ArraySegment<byte>>

    Question

  • Hello,

    I'm getting ArgumentException: "Field in TypedReferences cannot be static or init only." when trying to deserialize instance of the Timestamped<ArraySegment<byte>> structure.

    Following code can be used to reproduce the problem:

    byte[] array = new byte[] { 0x01, 0x02 };
    ArraySegment<byte> segment = new ArraySegment<byte>(array, 0, 2);
    Timestamped<ArraySegment<byte>> timestamped = new Timestamped<ArraySegment<byte>>(segment, DateTime.Now);
    
    byte[] serializedData;
    
    BinaryFormatter formatter = new BinaryFormatter();
    
    using (MemoryStream serializationStream = new MemoryStream())
    {
        formatter.Serialize(serializationStream, timestamped);
        serializedData = serializationStream.ToArray();
    }
    
    object deserialized;
    
    using (MemoryStream deserializationStream = new MemoryStream(serializedData))
    {
        deserialized = formatter.Deserialize(deserializationStream); //throws exception
    }

    Implementing ISerializable on Timestamped<T> struct or removing readonly modifier from its fields should resolve this problem. Im not sure, maybe there are better solutions?

    Best Regards,

      Hubert



    Friday, September 06, 2013 7:42 AM