考虑这两个类:
public Class Base {
public string Id {get; set;}
public string Name {get; set;}
public string LastName {get; set;}
}
以及派生类:
public Class Derived : Base {
public string Address {get; set;}
public DateTime DateOfBirth {get; set;}
}
When serializing the Derived class using Json.Net:
Derived record = new Derived record(); {// Initialize here...}
JsonConvert.SerializeObject(record);
默认情况下,Derived class的属性首先出现:
{
"address": "test",
"date_of_birth" : "10/10/10",
"id" : 007,
"name" : "test name",
"last_name": "test last name"
}
What I need:
{
"id" : 007,
"name" : "test name",
"last_name": "test last name"
"address": "test",
"date_of_birth" : "10/10/10",
}
Question
Is it possible to have the base class properties come first, when serializing the derived class (without using [JsonProperty(Order=)]
for each property of both classes)?