Код: Выделить всё
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(Manager), "Manager")]
[JsonDerivedType(typeof(Developer), "Developer")]
public class Employee
{
public string Type { get; set; }
public string Name { get; set; }
}
public class Manager : Employee
{
public string Phone { get; set; }
}
public class Developer : Employee
{
public string Laptop { get; set; }
}
Код: Выделить всё
[
{
"type": "Manager",
"name": "Bob",
"phone": "9876543210"
},
{
"type": "Developer",
"name": "Charlie",
"laptop": "MacBook Pro"
}
]
Код: Выделить всё
var jsonFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Employees.json");
var employeesContent = File.ReadAllText(jsonFilePath);
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
var employees = JsonSerializer.Deserialize(employeesContent, jsonSerializerOptions);

Пожалуйста, помогите мне, что мне не хватает.>
Подробнее здесь: https://stackoverflow.com/questions/798 ... nator-prop
Мобильная версия