在YamlDotNet中序列化子类

时间:2015-09-18 17:41:07

标签: .net serialization yamldotnet

我正在尝试在我的应用程序中添加YAML序列化/反序列化。我的类结构是这样的,几种类型派生自基类。当我尝试序列化其中一个子类时,看起来好像YamlDotNet不包含特定于子类的属性。作为简化示例的方式:

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

1 个答案:

答案 0 :(得分:0)

问题似乎是我的孩子班上的列表是一个只读属性,YAML序列化器不支持这个。