In my project, I have a class that has a property whose type can be inherited from:
public class Feed { ... [JsonProperty(TypeNameHandling = TypeNameHandling.Auto)] public FeedSource Source { get; set; } ... } public abstract class FeedSource { ... } public class CsvSource : FeedSource { ... } public class DbSource : FeedSource { ... }
I use the Entity Framework to load and store this object in the database, and I use Json.NET to serialize this object to JSON for further processing.
The problem I came across is that the $type property contains the name of the EF proxy type instead of the "real" typename. Therefore, instead of:
$type: "System.Data.Entity.DynamicProxies.CsvSource_0B3579D9BE67D7EE83EEBDDBFA269439AFC6E1122A59B4BB81EB1F0147C7EE12"
which does not make sense to other clients, I would like to receive:
$type: "MyNamespace.CsvSource"
in my JSON.
What is the best way to achieve this?
Dejan source share