为什么基类属性最后被序列化?

时间:2016-12-08 06:42:16

标签: c# json

我有一个基类

public class ParentSystemInfo
{
    private  string _Version = "R8.1";

    public SystemInfo()
    {

        this.Version = _Version;
    }

    }
    public string Version {

        get { return _Version; }
        set { _Version = value; }
    }    
}

我在另一个班级继承它

 public class ChildSystemInfo : ParentSystemInfo
{

    public ChildSystemInfo () :base()
    {

        this.MYInfo= new MYInfo();

    }
    public string Name { get; set; }
    public MYInfo MYInfo{ get; set; }      
}

当我序列化这个类时,我在json的末尾获得父属性而不是在开始时。为什么会发生这种情况,我该怎样才能阻止它。

1 个答案:

答案 0 :(得分:0)

您可以使用Json属性属性来控制正在搜索属性的顺序。请参阅此处的文档:http://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm

所以你会:

public class ParentSystemInfo
{
    private  string _Version = "R8.1";

    public SystemInfo()
    {
        this.Version = _Version;
    }

    // Setting the JsonProperty to be -1 will ensure it appears before
    // all properties for which this attribute was not set.
    [JsonProperty(Order = -1)]
    public string Version {
        get { return _Version; }
        set { _Version = value; }
    }    
}