XML 序列化具有抽象基类的可序列化对象的通用列表
有关如何使用抽象基类序列化通用对象列表的任何好示例。 XML Serialize可序列化对象的通用列表。我的基类类似于 Microsoft.Build.Utilities。任务
Any good sample on how to serialize list of generic objects with abstract base class. Samples with non abstract base class are listed in XML Serialize generic list of serializable objects. My base class is similar to Microsoft.Build.Utilities.Task
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种替代方法是使用
XmlElementAttribute
将已知类型列表移动到通用列表本身......这也将导致更好看的 XML 输出(没有丑陋的
xsi :type
属性)...Another alternative is to use the
XmlElementAttribute
to move the list of known types to the generic list itself...... which will also result in a better looking XML output (without the ugly
xsi:type
attribute)...拥有具有多个派生类型的抽象类通常很有用,以允许使用强类型列表等。
例如,您可能有一个抽象的 DocumentFragment 类和两个名为 TextDocumentFragment 和 CommentDocumentFragment 的具体类(此示例来自 Willis)。
这允许创建一个仅包含这两种类型的对象的 List 属性。
如果您尝试创建一个返回此列表的 WebService,您会收到一个错误,但这很容易通过下面的代码解决。XmlInclude
属性告诉该类它可能会序列化为这两个派生类。
这会在 DocumentFragment 元素中生成一个指定实际类型的属性,如下所示。
任何特定于派生类的附加属性也将使用此方法包含在内。
It is often useful to have abstract classes with several derived types to allow use of strongly typed lists and the such.
For example you might have a DocumentFragment class which is abstract and two concrete classes called TextDocumentFragment and CommentDocumentFragment (this example from Willis).
This allows the creation of a List property which can contain objects only of those two types.
If you attempt to create a WebService that returns this list you get an error but this is easy to get around with the code below....
The XmlInclude attributes tell the class that it might be serialized to those two derived classes.
This generates an attribute in the DocumentFragment element specifying the actual type, as below.
Any additonal properties specific to the derived class will also be included using this method.