How to serialize a custom class with YamlDotNet

十年热恋 提交于 2019-11-29 18:37:25

The default behavior of YamlDotNet is to serialize public properties and to ignore fields. The easiest fix is to replace the public fields with automatic properties:

public class Person
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    public Person(string first, string last)
    {
        FirstName = first;
        LastName = last;
    }
}

You could alter the behavior of YamlDotNet to serialize private fields relatively easily, but I do not recommend that.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!