Question

We are working on releasing version 2.0 of one of our products, and we want to maintain file compatibility with the installed base of version 1.0 customers. I have been implementing ISerializable on most of the types in our application, and seem to have hit a snag.

I have a Type, lets call it Family for the sake of discussion

[Serializable]
public class Family : 
    IDisposable,
    INotifyPropertyChanged
{
    private string m_Address;
    //....
    private List<Name> m_People;
    //...
}

This is what was shipped as version 1.0, in 2.0 we changed the Name class to a Person, which is effectively the same, but has a slightly different constructor, and obviously a different name.

[Serializable]
public class Family : 
    IDisposable,
    INotifyPropertyChanged,
    ISerializabe
{
    private string m_Address;
    //....
    private List<Person> m_People;
    //...

    private Family(SerializationInfo info, StreamingContext context)
    {
        m_Address = info.GetString("m_Address");
        m_People = (List<Person>)info.GetValue("m_People", typeof(List<Person>));
    }

    // <<GetObjectData Method>>
}

This obviously doesn't work, the Type "Name" is no longer in the assembly, so I added a SerializationBinder to fix that:

public sealed NamePersonSerializationBinder : SerializationBinder
{
    if (typeName.StartsWith("System.Collections.Generic.List`1[[Company.Name"))
        return typeof(List<Person>);
    else if (typeName.StartsWith("Company.Name"))
        return typeof(Person);
    else
        return null;
}

This mostly works, when I Deserialize the obeject, the binder is called, returns the correct type, but the Deserialization Constructor on Person never gets called. No exceptions are being thrown, anyone have any ideas?

Was it helpful?

Solution

Fixed! There is a base type of Person that was failing to finish it's constructor, which was causing the person constructor to never get called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top