79625171

Date: 2025-05-16 12:27:44
Score: 0.5
Natty:
Report link

Adding this three years later in case anybody else ends up here from a search.

Since .NET 7 there is a proper way of doing this. You just use the JsonDerivedType attribute on the base class, e.g.

[JsonDerivedType(typeof(TypeAClient))]
[JsonDerivedType(typeof(TypeBClient))]
[JsonDerivedType(typeof(TypeCClient))]
public class Client {
}

You have to include all the sub-types that you might want to serialise.

Reasons:
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: PaulH567