I have an abstract class named Extension
which has several derived classes such as DerivedExtensionA
, DerivedExtensionB
, etc.
Now I have a list defined as List<Extension>
which contains the derived classes instances.
Now if I serialize the above list, it only serializes the base class properties that are in Extension
since the list has the base class Extension
type. If I define the list as List<DerivedExtensionA>
and then put only instances of DerivedExtensionA
in it, then they are serialized fine. But my code is generic which is supposed to accept all types of Extensions, so this isn't a workable solution for me.
So question is ..
How do I keep the list defined as List<Extension>
and still be able to fully serialize the contained derived class instances that contain ALL their properties ?
Here is a fiddle showing this behavior: https://dotnetfiddle.net/22mbwb
EDIT: Corrected the fiddle URL
JsonConverter<Extension>
that serializes the derived classes explicitly as shown in Serialize objects implementing interface with System.Text.Json, or 2) serialize yourList<Extension>
via anIEnumerable<object>
surrogate property as shown in System.Text.Json serialize derived class property. – Abet