首页 > 解决方案 > 如何使用 yamldotnet 启用解析 yaml,它接受同名的列表和键值对?

问题描述

我有一个 yaml,以下两种格式都可以接受 -

  args:
    buildno: 1
    gitcommithash: cdc3b19

  args:
    - buildno=1
    - gitcommithash=cdc3b19

以目前的YamlDotnet配置,

[YamlMember(Alias = "args")]
public Dictionary<string, string> Args { get; set; }

当 yaml 的格式为 no 时,我可以解析。1. 如何修改我的YamlDotNet代码以接受这两种格式?即接受列表以及 Args 字典?

标签: c#yamlyamldotnet

解决方案


The easiest way to do that is to create a class that implements both IDictionary<string, string> and IList<string>. You could try inheriting from Dictionary<string, string> and implement the list interface. I think it would be enough to implement the Add method from IList, where you split the string by the = character and add to the dictionary.

There are other ways but this is the simplest.


推荐阅读