我有一个前端应用程序,它正在生成一个SON对象(让我们称之为RuleGroup
).每个RuleGroup
包含子组或 node 的列表.
以下是一个大致的例子:
{
"combinator": "OR", <---RuleGroup
"not": true,
"criteria": [
{
"field": "path", <---RuleCriteria
"key": "",
"value": "",
"operator": "="
},
{
"combinator": "AND", <---RuleGroup
"not": false,
"criteria": [
{
"field": "header", <---Node
"key": "",
"value": "",
"operator": "="
},
]
}
]
}
如何在API控制器上对这样的对象进行子化?
我try 过设置一些类似的类型,但我的条件列表最终充满了BaseRule
个无法转换为其他两种类型的对象.
public class RuleGroup : BaseRule
{
public string Combinator { get; set; }
public bool Not { get; set; }
public List<BaseRule> Criteria { get; set; }
}
public class RuleCriteria : BaseRule
{
public string Field { get; set; }
public string Operator { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
public class BaseRule
{
}
我怀疑我需要的是https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-8-0#polymorphic-type-discriminators之类的东西,但我真的不想向我的数据中添加专用的类型字段.
我的最后一个 Select 是手动将数据重新子化回适当的对象中.
如果有一种方法可以在我自己滚动的情况下做到这一点,请告诉我.
谢谢