【发布时间】:2020-07-03 11:03:32
【问题描述】:
我有这种情况:
public class Dto
{
public int TypeId { get; set; }
public IType Type { get; set; }
}
public class Type1 : IType
{
public string PropertyA { get; set; }
}
public class Type2 : IType
{
public int PropertyB { get; set; }
public bool PropertyC { get; set; }
}
public class MyController : ApiController
{
[HttpPost]
public IHttpActionResult Post(Dto dto)
{
}
}
如何根据 TypeId 属性的值对 IType 接口的正确实现进行反序列化?
我尝试使用 JsonConverter(以下示例:https://gist.github.com/teamaton/bba69cf95b9e6766f231),但我只能指定一种具体类型:
public class Dto
{
public int TypeId { get; set; }
[JsonConverter(typeof(ConcreteTypeConverter<Type1>)]
public IType Type { get; set; }
}
【问题讨论】:
标签: c# .net api model-view-controller