我仍然试图围绕整个xml序列化事物包围我的脑子,看起来我又需要一些帮助了.
我需要能够反序列化抽象类型的属性.这种类型将随着时间的推移而添加许多不同的具体类型,并且在许多不同的模型中被引用,因此明确列出每种具体类型并不是理想的解决方案.
我已经阅读了XML Serialization和Inherited Types线程,并提出了以下内容:
<Page>
<introCommand>
<PlayElement />
</introCommand>
</Page>
Run Code Online (Sandbox Code Playgroud)
**
namespace TestService
{
public class Page
{
[XmlElement("introCommand", Type = typeof(XmlCommandSerializer<AbstractCommandModel>))]
//[XmlElement(typeof(PlayElement))] **NOTE: the example works if I use this instead
public AbstractCommandModel introCommand;
}
}
Run Code Online (Sandbox Code Playgroud)
**
namespace TestService
{
public class AbstractCommandModel
{
}
}
Run Code Online (Sandbox Code Playgroud)
**
namespace TestService
{
public class PlayElement : AbstractCommandModel
{
}
}
Run Code Online (Sandbox Code Playgroud)
**
namespace TestService
{
public class XmlCommandSerializer<AbstractCommandModel> : IXmlSerializable
{
// Override the Implicit Conversions Since the XmlSerializer …Run Code Online (Sandbox Code Playgroud)