自定义类型和序列化列表的问题

发布于 2024-10-10 11:38:42 字数 1392 浏览 1 评论 0原文

我们正在努力发布其中一款产品的 2.0 版,并且希望保持与 1.0 版客户的安装基础的文件兼容性。我已经在应用程序中的大多数类型上实现了 ISerializable,但似乎遇到了障碍。

我有一个 Type,为了讨论起见,我们将其称为 Family

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

这是 1.0 版发布的内容,在 2.0 中,我们将 Name 类更改为 Person,这实际上是相同的,但构造函数略有不同,并且显然是不同的名字。

[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>>
}

这显然不起作用,类型“名称”不再在程序集中,所以我添加了一个 SerializationBinder 来解决这个问题:

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;
}

这主要有效,当我反序列化对象时,调用绑定器,返回正确的类型,但是Person 上的反序列化构造函数永远不会被调用。没有例外,有人有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2024-10-17 11:38:42

固定的!有一个 Person 的基本类型无法完成其构造函数,这导致 person 构造函数永远不会被调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文