简体   繁体   English

反序列化为其名称空间已更改的类型

[英]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.

相关问题 如何针对已更改的类型反序列化旧数据? - How do I deserialize old data for a type that has changed? 更改名称空间后,DataContractSerializer无法反序列化 - DataContractSerializer cannot deserialize after namespace changed 如何反序列化没有命名空间等的xml ...? - How to deserialize an xml that has no namespace, etc…? 使用JSON.NET反序列化类型为接口的列表 - Using JSON.NET to deserialize a list whose type is an interface DataContractSerializer:由于属性类型更改而无法反序列化 - DataContractSerializer: can't deserialize because of changed property type 嵌套集合 - 当实体名称空间已更改时发出问题 - Nested collections - issue,when namespace of entity has been changed 如何反序列化二进制格式的 object,其类型被修改为抽象 class? - How to deserialize a binary formatted object whose type got modified as an abstract class? 用名称空间反序列化xml数组 - deserialize xml array with namespace 无法使用命名空间反序列化XmlArray - Cannot Deserialize XmlArray with Namespace 将JSON反序列化为类失败,并显示“加载类型时发生故障” - Deserialize JSON into classes fails with “Failure has occurred while loading a type”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM