如何将包含字符串的 Yaml 对象反序列化为 List<string>?

How can I deserialize a Yaml object containing strings into a List<string>?

我创建了一个带有文件名的 Yaml,所以我可以让我的程序检查列表中的每个文件是否存在。我还没有对 yaml 做很多事情,文档对我没有真正的帮助。

这是我的 Yaml(非常小):

DLLs:
    - Filename1
    - Filename2
    - Filename3

目前,这是我的代码:

using (var reader = new StringReader(File.ReadAllText("./Libraries/DLLList.yml")))
{
    /*
     * List<string> allDllsList = deserialized yaml.getting all values of the "DLLs"-list
     */

    var deserializer = new Deserializer();

    var dlls = deserializer.Deserialize<dynamic>(reader)["DLLs"] as List<Object>;
    /*This gives me the Error "Object System.Collections.Generic.Dictionary`2[System.Object,System.Object] cannot be converted into "System.String""*/
    List<string> allDllsList = dlls.Cast<String>().ToList();
}

有人可以向我解释一下如何从 Yaml 文件中获取值,以及为什么它以您的方式工作吗?

编辑:现在可以了,我用错了 yaml,我有 2 个版本

首先,从 deserializer.Deserialize<dynamic>(reader) 中获取 return 值并在调试器中检查它。它是一个 Dictionary<String, Object>,它有一个名为 "DLLs" 的条目,其中包含一个 List<Object>。该列表中的对象都是字符串。给你:

var dlls = deserializer.Deserialize<dynamic>(reader)["DLLs"] as List<Object>;

//  Use .Cast<String>() as shown if you want to throw an exception when there's something 
//  that does not belong there. If you're serious about validation though, that's a bit 
//  rough and ready. 
//  Use .OfType<String>() instead if you want to be permissive about additional stuff 
//  under that key.
List<string> allDllsList = dlls.Cast<String>().ToList();