在 YamlDotNet 中序列化 Child 类

Serializing Child Classes in YamlDotNet

我正在尝试在我的应用程序中添加 YAML serializing/deserializing。我的 class 结构是这样的,几个类型派生自基数 class。当我尝试序列化其中一个子 class 时,YamlDotNet 似乎不包含特定于子 class 的属性。作为一个简化的例子:

Public Class BaseGrammar
    <YamlIgnore>
    Public Property FilePath As String

    Public Overridable Property Name As String
    Public Overridable Property Author As String
    Public Overridable Property Description As String
    Public Overridable Property Tags As BindingList(Of String)
End Class

Public Class AssignmentGrammar
    Inherits BaseGrammar

    Private _items As BindingList(Of  Item)

    Public ReadOnly Property Items As BindingList(Of Item)
    Get
        Return _items
    End Get
End Class

Public Class Item
    Public Property Name As String
    Public Property Value As String
End Class

使用以下代码序列化:

Public Shared Sub ConvertXMLToYAML(ByVal sourcefileName As String,
                                   ByVal targetFileName As String)
    Dim grammar As BaseGrammar = OpenXML(sourcefileName)
    Dim serializer As New YamlDotNet.Serialization.Serializer(YamlDotNet.Serialization.SerializationOptions.Roundtrip)
    Using output As IO.TextWriter = IO.File.CreateText(targetFileName)
        serializer.Serialize(output, grammar)
    End Using
End Sub

产生以下 YAML:

!Grammars.Assignment.AssignmentGrammar,%20Grammars,%20Version=1.0.0.1,%20Culture=neutral,%20PublicKeyToken=null
Name: Urban Gangs
Author: Lance Boudreaux
Description: Generates gangs for urban adventures.
Tags:
- Gang
- Encounter

看来问题是我在 child class 上的列表是只读的 属性 而 YAML 序列化程序不支持这个。