0

Well, several releases later, the key in the dictionary has changed from int to long. Now during deserialization of an old object graph, Deserialize method aborts with ArgumentException and famous "Object of type XX cannot be converted to type YY" exception.

The complete exception is :

Object of type 'System.Collections.Generic.Dictionary`2[System.Int32,System.Collections.Generic.List`1[XX]]' cannot be converted to type 'System.Collections.Generic.Dictionary`2[System.Int64,System.Collections.Generic.List`1[XX]]'.

Worth to note that I'm using a SerializationBinder class for handling type changes during releases.

Any Help how to handle this error?

legoscia
  • 39,593
  • 22
  • 116
  • 167
BHP
  • 443
  • 4
  • 13
  • 2
    This is why it is not recommended to use `BinaryFormatter` for persisted data. I don't know how to fix it, that's why I don't ever use it for data that is persisted or transmitted between machines. Please include what you have done inside your `SerializationBinder` if you would like help fixing it. – Scott Chamberlain Jan 29 '16 at 17:28
  • Try BinarySerializer from nuget – Jeff Jan 29 '16 at 17:52
  • @Scott, In the SerializationBinder::BindToType method, I just remap old type names to new ones, those who has changed, renamed, etc. nothing especial. – BHP Jan 29 '16 at 19:36
  • @Jeff, I will give it a try. Thanks. – BHP Jan 29 '16 at 19:40
  • Are you converting all your `Dictionary>` fields to have `long` keys, or just some? If you're doing it in just one or two places, see http://stackoverflow.com/questions/30021070/changing-types-during-binary-deserialization-in-c-sharp – dbc Jan 29 '16 at 21:17
  • Also, if you want to access the data of a dictionary during binary deserialization, you will need to call its `OnDeserialization` method manually first. See https://stackoverflow.com/questions/25582842/dictionary-is-empty-on-deserialization – dbc Jan 29 '16 at 21:20

1 Answers1

0

The "proper" fix (assuming you don't have other legacy systems accessing this data) is going to be "deserializing" all of the stored data into a Dictionary<int,List> object, then copying each of those elements into a Dictionary<long,List> object, then serializing the Int64 dictionary back to the storage medium.

That would provide an immediate fix to the problem, without having to change any code but could be resource intensive, depending on how much data you're dealing with.

Mike U
  • 709
  • 6
  • 11
  • @ Mike, Well, This is a very HUGE application, with lots of types. The object graph depth is about 6 and every level contains many types. This exception occurs just for one of them, so the fix may not be possible. – BHP Jan 29 '16 at 19:38
  • I was afraid of that. I don't know any other way, after using binary serialization. – Mike U Feb 01 '16 at 15:42