如何正确反序列化子类?
public class FilterExpression : BaseExpression
{
public string Selector { get; set; }
public FilterOperation Operation { get; set; }
public object Value { get; set; }
}
public class GroupExpression : BaseExpression
{
public GroupOperator Operator { get; set; }
public List<BaseExpression> Expressions { get; set; }
}
public class BaseExpression
{ }
public static GroupExpression Deserialize(string filterJson)
{
return JsonConvert.DeserializeObject<GroupExpression>(filterJson);
}
public void tes(){
string jsonString = ""{\"Operator\":\"and\",\"Expressions\":[{\"Selector\":\"UserName\",\"Operation\":\"=\",\"Value\":\"admin\"},{\"Selector\":\"CreatedAt\",\"Operation\":\"=\",\"Value\":\"2018-01-01\"},{\"Operator\":\"and\",\"Expressions\":[{\"Selector\":\"UserName\",\"Operation\":\"=\",\"Value\":\"admin\"},{\"Selector\":\"CreatedAt\",\"Operation\":\"=\",\"Value\":\"2018-01-01\"}]}]}"";
var result = FilterExpression.Deserialize(jsonString);
}
我希望result的输出是Expression的子对象(FilterExpression或GroupExpression),但实际输出是BaseExpression对象类型。