none
Unable to deserialize binary data RRS feed

Answers

  • BinarySerializer stores information about type of the value. Output stream, which is generated by serializer from my code, contains information about IComparable field that has a System.Decimal value. BinarySerializer operates with instances by it runtime type information (obtained with help of System.Object.GetType()), not by field type. Therefore, value of the IComparable field stored as System.Decimal value. BinarySerializer knows about this fact. It obtains actual type from stream (it is a System.Decimal), deserialize it and store in the IComparable field. This bug is associated with stream parsing, because any non-binary data (if IComparable field contains string literal) can be serialized and deserialized successfully. After deserialization into System.Decimal value, BinarySerializer just calls GetField("m_value").SetValue(this, [decimal value]). Cast from System.Decimal to IComparable provides by Reflection an runtime.
    E' più facile spezzare un atomo che un pregiudizio
    • Marked as answer by Bruno Yu Monday, June 30, 2008 7:57 AM
    Tuesday, June 24, 2008 11:39 PM

All replies

  • The C# compiler is smart enough to generate the code required to box and cast a System.Decimal to an IComparable interface reference.  BinaryFormatter is not nearly smart enough to do the same.  It is easy enough to make serialization fail as well, just pass an object to the constructor that isn't serializable.  If you want to salvage this you'll probably need to use custom deserialization code that casts back to IComparable.
    Hans Passant.
    • Proposed as answer by Bruno Yu Monday, June 30, 2008 7:56 AM
    Tuesday, June 24, 2008 12:11 PM
    Moderator
  • BinarySerializer stores information about type of the value. Output stream, which is generated by serializer from my code, contains information about IComparable field that has a System.Decimal value. BinarySerializer operates with instances by it runtime type information (obtained with help of System.Object.GetType()), not by field type. Therefore, value of the IComparable field stored as System.Decimal value. BinarySerializer knows about this fact. It obtains actual type from stream (it is a System.Decimal), deserialize it and store in the IComparable field. This bug is associated with stream parsing, because any non-binary data (if IComparable field contains string literal) can be serialized and deserialized successfully. After deserialization into System.Decimal value, BinarySerializer just calls GetField("m_value").SetValue(this, [decimal value]). Cast from System.Decimal to IComparable provides by Reflection an runtime.
    E' più facile spezzare un atomo che un pregiudizio
    • Marked as answer by Bruno Yu Monday, June 30, 2008 7:57 AM
    Tuesday, June 24, 2008 11:39 PM
  • It sounds like you already know more about it than anyone else.  Why don't you post to the Connect website to report this bug.  Don't get your hopes up for a quick fix though.
    Hans Passant.
    Tuesday, June 24, 2008 11:58 PM
    Moderator