【发布时间】:2020-12-22 14:08:13
【问题描述】:
我有这个 Web Api 控制器操作:
public Plant<TissueProductionLine> GetPlant(string plant, string range)
{
return _repo.GetPlant(plant, range);
}
启动它我明白了:
不应使用数据合同名称“IGMProdLineDashboard.Models”键入“IGMProdLineDashboard.Models.TissueProductionLineGroup”。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。
好的,没问题,我将用[KnownType] 属性装饰我的Plant 对象并指定非原始属性:
[KnownType(typeof(ProductionLineGroups<T>))]
public class Plant<T>: IPlant<T> where T : IProductionLine
{
public Plant ()
{
ProductionLineGroups = new ProductionLineGroups<T>();
}
public ProductionLineGroups<T> ProductionLineGroups { get; set; }
现在我得到一个编译时错误:
'IGMProdLineDashboard.Models.ProductionLineGroups':属性参数不能使用类型参数
重读第一条消息,想知道派生类型:TissueProductionLineGroup
如果我用这个装饰我的 Plant 对象,一切正常:
[KnownType(typeof(TissueProductionLineGroup))]
显然这里的含义是我的Plant 现在需要了解所有不同类型的生产线,这意味着每当添加新组时我都需要更新Plant。
有没有办法解决这个问题?
【问题讨论】:
标签: c# asp.net asp.net-web-api