反序列化来自不同程序集的匹配类型 [英] Deserialize matching types from different assemblies
问题描述
如果我在名称空间AssemblyA.Entities
下的AssemblyA
中定义了一个类/接口对:
If I have a class/interface pair defined in AssemblyA
under namespace AssemblyA.Entities
:
public IEntity
{
string Name { get; set; }
}
[Serializable]
public Entity : IEntity
{
public string Name { get; set; }
}
然后我使用XmlSerializer
将其序列化为XML:
And I serialize it to XML using XmlSerializer
:
var myEntities = new List<IEntity>();
// myEntities is populated somehow
var data = XmlSerializationManager.Serialize(myEntities);
// 'data' gets saved to disk somewhere as a file
然后,如果我将代码/命名空间复制到AssemblyB
中,以便拥有命名空间AssemblyA.Entities
和完全相同的代码:
Then, if I duplicate the code/namespaces into AssemblyB
so that I have namespace AssemblyA.Entities
and the exact same code:
public IEntity
{
string Name { get; set; }
}
[Serializable]
public Entity : IEntity
{
public string Name { get; set; }
}
如果我尝试反序列化先前已序列化的XML,我是否会返回AssemblyB
的AssemblyA.Entities.IEntity
列表?还是会失败?
If I attempt to deserialize the previously serialized XML, will I get back AssemblyB
's list of AssemblyA.Entities.IEntity
? Or will it fail?
串行器在什么时候停止关心反序列化的内容?组装可以不同吗?命名空间可以不同吗?只要属性的名称相同,类型名称是否重要?
At what point does the serializer stop caring about what it's deserializing to? Can the assembly differ? Can the namespaces differ? Do the type names matter so long as the properties are named the same?
推荐答案
这将起作用.您将获得AssembyB实体.
This will work. You will get AssembyB entity.
从本质上讲,这就是Web服务如何工作的,即客户端将根据wsdl中的信息对类进行脚手架,然后客户端将来自soap消息的数据反序列化为这些脚手架的类.
This is essentially how Web services work when the client would scaffold classes based on information in the wsdl, the client would then deserialise the data from the soap message into these scaffolded classes.
这篇关于反序列化来自不同程序集的匹配类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!