[英]Deserialize to type whose namespace has changed
Using .NET 4/C#... 使用.NET 4 / C#...
I need to deserialize old config files that contain the serialized representation of a type named, say, ns1.X . 我需要反序列化包含名为ns1.X的类型的序列化表示的旧配置文件。 The serialization has been done using
BinaryFormatter
. 序列化已使用
BinaryFormatter
完成。
The problem is, after a round of refactoring, the type X has been moved to a different namespace, say ns2.X . 问题是,经过一轮重构后,类型X已被移动到不同的命名空间,比如说ns2.X。
I have tried creating a new empty ns1.X type that derives from ns2.X, and while this circumvents the ' Can't find type ns1.X ' error, the properties in the deserialized object are all null
. 我尝试创建一个从ns2.X派生的新的空ns1.X类型,虽然这绕过了' 找不到类型ns1.X '错误,但反序列化对象中的属性都是
null
。 Also, no ctors get called in the process. 此外,在此过程中没有调用ctors。
Any suggestions? 有什么建议?
You're going to need to use an ISerializationSurregate to make this happen. 您将需要使用ISerializationSurregate来实现这一目标。 It's not too difficult and Jeff Richter explains it really well.
这并不困难,杰夫里希特解释得非常好。 I recommend his book CLR Via c# 3.
我推荐他的书CLR Via c#3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.