Deserialization problems: System.Drawing.Color
-
Hi all, I've used the default .NET serialization for a class with a System.Drawing.Color member. The code is now in use by people, and I need to add an extra member to the class, but still deserialize older versions. It uses the SoapFormatter, so can't use the [OptionalField] attribute. So I added ISerializable to the class, which instantly caused a much bigger problem: Structs (like System.Drawing.Color) suddenly can no longer be deserialized; I get the exception message, "Top Object cannot be instantiated for element 'foreColor'". Does anyone know how I can get the SoapFormatter to read both old and new versions, and still handle structs? Thanks! Alan
-
Hi all, I've used the default .NET serialization for a class with a System.Drawing.Color member. The code is now in use by people, and I need to add an extra member to the class, but still deserialize older versions. It uses the SoapFormatter, so can't use the [OptionalField] attribute. So I added ISerializable to the class, which instantly caused a much bigger problem: Structs (like System.Drawing.Color) suddenly can no longer be deserialized; I get the exception message, "Top Object cannot be instantiated for element 'foreColor'". Does anyone know how I can get the SoapFormatter to read both old and new versions, and still handle structs? Thanks! Alan
Maybe following design could help: Create a new class by extending your previous class, i.e.
public class MyNewClass : MyOldClass
Then add a Version property to the (new) class. For deserialization, get that property withXmlNode versionNode = xmlDoc.SelectSingleNode("/MyNewClass/Version");
If versionNode is null, call the deserialization method of MyOldClass, otherwise use the deserialization method of MyNewClass. With the version property, you will then be able to extend further easily.