如何动态反序列化子类?

时间:2019-04-15 11:24:55

标签: c# asp.net-core json.net deserialization .net-standard

如何正确反序列化子类?

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对象类型。

0 个答案:

没有答案