2

我需要将以下 YAML 反序列化为我的自定义类型。YamlAlias 属性似乎已经过时,所以我将其替换为 YamlMember。它在反序列化以下 YAML 时失败,但有以下例外:

    host:
      properties:
        mem_size: 2048  MB

YamlDotNet.Core.YamlException : (Line: 21, Col: 13, Idx: 524) - (Line: 21, Col: 13, Idx: 524): 反序列化期间出现异常----> System.Runtime.Serialization.SerializationException :在类型“Toscana.Domain.HostProperties”上找不到属性“mem_size”。

public class Host
{
    public HostProperties Properties { get; set; }
}

public class HostProperties
{
    [YamlMember(typeof(DigitalStorage))]
    public string MemSize { get; set; }
}
4

1 回答 1

4

Alias是类的属性,YamlMemberAttribute它不在构造函数中。现在,我不知道您的DigitalStorage类的外观以及 a 是否string会成功反序列化(我对此表示怀疑),但是由于您的问题是添加别名,因此您是这样做的:

public class HostProperties
{
    [YamlMember(typeof(DigitalStorage), Alias = "mem_size")]
    public string MemSize { get; set; }
}
于 2016-06-14T10:27:13.590 回答