Как включить (или заменить) статические свойства в System.text.json Serialization?C#

Место общения программистов C#
Anonymous
Как включить (или заменить) статические свойства в System.text.json Serialization?

Сообщение Anonymous »

У меня есть какой -то объект, как это: < /p>

Код: Выделить всё

public class ResponseBase
{
public string eventType { get; }
public string eventSourceGuid { get; }
}

public class QueryDevicesResponse : ResponseBase
{
public new static string eventType { get => "queryDevicesResponse"; }
public new string eventSourceGuid { get => "0"; }
public EventData eventData { get; set; }
}
< /code>
eventType
field is static because I'm trying to:
  • minimize lines of code
  • use it like "application-wide" string stored (in source code) together with DTO class definition, and use it in some switch and if statements without instantiating QueryDevicesResponse.
When Я звоню: < /p>

Код: Выделить всё

QueryDevicesResponse queryDevicesResponse = QueryDevicesResponse.Mock();
JsonSerializer.Serialize.JsonSerializer.Serialize(queryDevicesResponse);
< /code>
I'm getting JSON without eventType
Поле. Я предполагаю, что это потому, что поле статическое.
Могу ли я изменить поведение Jsonserializer, чтобы включить также статические поля? /> Альтернативно:
Как я могу заменить static модификатор, чтобы получить аналогичное поведение и сохранить код маленьким?


Подробнее здесь: https://stackoverflow.com/questions/646 ... ialization

Вернуться в «C#»