Deserialize matching types from different assemblies

谁说我不能喝 提交于 2019-12-25 00:46:19

问题


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

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

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

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?


回答1:


This will work. You will get AssembyB entity.

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.



来源:https://stackoverflow.com/questions/26368990/deserialize-matching-types-from-different-assemblies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!