views:

22

answers:

1

I've had a class which looked like

public class MyClass
{
    public string EmployerName;
    public string EmployerSurname;
    public string EmploeeName;
    public string EmploeeSurname;
}

I've refactored the code above to this:

public class MyClass
{
    public MyClass()
    {
        Employer = new PersonInfo();
        Emploee = new PersonInfo();
    }

    public class PersonInfo
    {
        public string Name;
        public string Surname;
    }
    public PersonInfo Emploee;
    public PersonInfo Employer;

    [Obsolete]
    public string EmploeeName
    {
        get
        {
            return Emploee.Name;
        }
        set
        {
            Emploee.Name = value;
        }
    }
    [Obsolete]
    public string EmploeeSurname
    {
        get
        {
            return Emploee.Surname;
        }
        set
        {
            Emploee.Surname= value;
        }
    }
    [Obsolete]
    public string EmployerName
    {
        get
        {
            return Employer.Name;
        }
        set
        {
            Employer.Name = value;
        }
    }
    [Obsolete]
    public string EmployerSurname
    {
        get
        {
            return Employer.Surname;
        }
        set
        {
            Employer.Surname = value;
        }
    }

Problem is, that when deserializing XMLs, which were serialized from old class version, I hoped that the new properties would work, and fields of the inner objects would be filled, but they don't.

Any ideas how, besides implementing IXmlSerializable, I could modify the new class to support both new and old versions of XMLs? Or maybe IXmlSerializable is the only way?

+2  A: 

Do you only want to support the old ones for deserialization? if so, you could have:

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public string EmploeeName {
    get { return Emploee.Name; }
    set { Emploee.Name = value; }
}
public bool ShouldSerializeEmploeeName() { return false;}

The bool method tells XmlSerializer never to write this, but it will still be read. [Browsable] tells it not to appear in things like DataGridView, and [EditorBrowsable] tells it not to appear in intellisense (only applies to code referencing the dll, not the project, nor code in the same project).

Marc Gravell