【问题标题】:Custom Serialization of Inherited List继承列表的自定义序列化
【发布时间】:2012-10-17 11:20:59
【问题描述】:

如果我有一个从通用列表继承的基类,并且派生类需要使用列表条目的自定义名称进行序列化,我该如何输入正确的序列化属性和/或引用基列表?

Public MustInherit Class SpecialList(Of T)
   Inherits List(Of T)
   'Other methods here
End Class

Public Class Cache
   Inherits SpecialList(Of CacheEntry)

   <XmlElementAttribute("CustomName")> _
   Public Property Entries() As List(Of CacheEntry)
      Get
         Return ???
      End Get
      Set(value As List(Of CacheEntry))
         ??? = value
      End Set
   End Property
End Class

这是否真的可以在不覆盖基类方法、编写自定义序列化或实现 IXmlSerializable 的情况下实现?

我希望得到类似这样的输出 XML:

<cache>
    <customname></customname>
    <customname></customname>
</cache>

【问题讨论】:

  • 是的,看上面的代码,它不会编译

标签: c# vb.net inheritance xml-serialization


【解决方案1】:

XmlRootAttribute 添加到CacheEntry

<XmlRootAttribute(ElementName:="customname")>
Public Class CacheEntry

作为一个附带问题,如果Cache 继承自List(通过SpecialList),那么为什么你有一个Entries 属性也是List

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多