DataContract with Json.Net

https://www.newtonsoft.com/json/help/html/DataContractAndDataMember.htm

如果class使用了DataContract,name没有使用DataMember的property就不解析

[DataContract]
public class File
{
    // excluded from serialization
    // does not have DataMemberAttribute
    public Guid Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Size { get; set; }
}

File file = new File
{
    Id = Guid.NewGuid(),
    Name = "ImportantLegalDocuments.docx",
    Size = 50 * 1024
};

string json = JsonConvert.SerializeObject(file, Formatting.Indented);

Console.WriteLine(json);
// {
//   "Name": "ImportantLegalDocuments.docx",
//   "Size": 51200
// }
原文地址:https://www.cnblogs.com/chucklu/p/10431481.html