How to serialize a custom class with YamlDotNet

前端 未结 1 1992
鱼传尺愫
鱼传尺愫 2020-12-22 03:13

I\'m trying to serialize a custom class with YamlDotNet library.
Here is my class:

public class Person
{
    stri         


        
相关标签:
1条回答
  • 2020-12-22 03:31

    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.

    0 讨论(0)
提交回复
热议问题