我目前正在尝试使用 YamlDotNet 库将 YAML 文档反序列化为标准 .NET 对象,例如string
标量值和映射。Dictionary<string, object>
我猜这个Deserializer
类是最好的选择,但它的输出是object
and Dictionary<object>
。我尝试INodeTypeResolver
像这样实现自定义:
class MyNodeTypeResolver : INodeTypeResolver
{
bool INodeTypeResolver.Resolve(NodeEvent nodeEvent, ref Type currentType)
{
if (currentType == typeof(object))
{
if (nodeEvent is SequenceStart)
currentType = typeof(List<object>);
else if (nodeEvent is MappingStart)
currentType = typeof(Dictionary<string, object>);
else if (nodeEvent is Scalar)
currentType = typeof(string);
return true;
}
return false;
}
}
并像这样使用它:
Deserializer deserializer = new Deserializer();
deserializer.TypeResolvers.Add(new MyNodeTypeResolver());
var res = deserializer.Deserialize(input);
但这似乎没有任何效果。有什么办法可以改变产生的对象的类型Deserializer
吗?