Serialization of StringDictionary

  • Thread starter Thread starter J.R
  • Start date Start date
J

J.R

I'm getting an error when I attempt to serialize a type StringDictionary,
error suggests type not marked for Serialization, could someone tell me how
to go about this?

Thanks.
 
In short, you can't. The type isn't marked as Serializable and doesn't
implement ISerializable.

As an alternative, you can use a ListDictionary. ListDictionaries are
serializable. Only difference is they take any type of object, not just
strings.

The other alternative is to write a custom string dictionary that is
serializable.

-Rob [MVP]
 
J.R.
In addition to Rob's suggestions.

You could manually iterate the StringDictionary in the containing class
adding the items to the SerializationInfo class in the GetObjectData method.
This would assume the containing class implements the ISerializable
interface (in addition to having the SerializableAttribute. I would add a
number of items count before saving the items. So on deserialization I would
know how many items to restore. Just remember the names you use in
SerializationInfo need to be unique, I would probably prefix the items &
count from the StringDictionary with some unique value.

Hope this helps
Jay
 
Thank you kind Sirs, for your prompt and informative responses. Once again,
I've implemented your suggestions and it is working perfectly.

John.
 
Back
Top