views:

649

answers:

3

I'm refactoring some objects that are serialized to XML but need to keep a few properties for backwards compatability, I've got a method that converts the old object into the new one for me and nulls the obsolete property. I want to use the Obsolete attribute to tell other developers not to use this property but it is causing the property to be ignored by the XmlSerializer.

Similar Code:

[Serializable]
public class MySerializableObject
{
    private MyObject _oldObject;
    private MyObject _anotherOldObject;

    private MyObject _newBetterObject;

    [Obsolete("Use new properties in NewBetterObject to prevent duplication")]
    public MyObject OldObject
    {
      get { return _oldObject; }
      set { _oldObject = value; }
    }

    [Obsolete("Use new properties in NewBetterObject to prevent duplication")]
    public MyObject AnotherOldObject
    {
      get { return _anotherOldObject; }
      set { _anotherOldObject = value; }
    }

    public MyObject NewBetterObject
    {
      get { return _anotherOldObject; }
      set { _anotherOldObject = value; }
    } 
}

Any ideas on a work around? My best solution is to write obsolete in the xml comments...

Update: I'm using .NET 2.0

A: 

EDIT: After reading a MS Connect article, it appears that .Net 2.0 has a 'feature' where it makes ObsoleteAttribute equivalent to XmlIgnoreAttribute without any notification in the documentation. So I'm going to revise my answer to say that the only way to have your cake and eat it too in this instance is to follow @Will's advice and implement serialization manually. This will be your only future proof way of including Obsolete properties in your XML. It is not pretty in .Net 2.0, but .Net 3.0+ can make life easier.

From XmlSerializer:

Objects marked with the Obsolete Attribute no longer serialized In the .NET Framework 3.5 the XmlSerializer class no longer serializes objects that are marked as [Obsolete].

sixlettervariables
He's looking for a workaround.
Will
@Will: it does pose a strange problem seeing as how .Net 2.0 should allow it.
sixlettervariables
I'm also using .net2 of which has no mention of this in the same article for the correct version
Rob Stevenson-Leggett
I think when we move from .NET 2 we'll drop the backwards compatability anyway so it won't be a problem.
Rob Stevenson-Leggett
A: 

1) WAG: Try adding the XmlAttributeAttribute to the property; perhaps this will override the ObsoleteAttribute
2) PITA: Implement IXmlSerializable

Will
+3  A: 

You may try the following workaround:

add a method named

ShouldSerializeOldObject ()
{
   return true;
}

ShouldSerializeAnotherOldObject ()
{
   return true
}

this may override the obsolete Attribute

Calamitous
Not sure I understand what you're suggesting here...
Rob Stevenson-Leggett
by adding a method "ShouldSerialize*" you tell the XmlSerializer if you want the Property to be serialized (normally used in cases where - i don't know - you just want to serialize an int value if it's larger than 5it MAY work here too
Calamitous
Ah thanks, I'll give it a try
Rob Stevenson-Leggett