自定义类型和序列化列表的问题
我们正在努力发布其中一款产品的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
固定的!有一个 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.