反序列化:接口,抽象类
[DataContract] public abstract class ProductPromotionDetail { }
[JsonConverter(typeof(ConcreteConverter<ProductPromotionDetail_4>))] public ProductPromotionDetail Detail { get; set; }
public class ProductPromotionDetail_4 : ProductPromotionDetail {}
public class ConcreteConverter<T> : JsonConverter { public override bool CanConvert(Type objectType) => true; public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return serializer.Deserialize<T>(reader); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, value); } }
Refer:https://www.c-sharpcorner.com/UploadFile/20c06b/deserializing-interface-properties-with-json-net/