我有一个Api返回JSON数据作为响应。这种反应基本上是对调查的回答。我们有多项调查。每项调查都有多个问题,可能与其他调查不同。 (希望我在这里有意义)
我根据调查结果创建了一个模型类,如下所示: -
public string value1 { get; set; }
public string value2 { get; set; }
public string value3 { get; set; }
public string value4 { get; set; }
public int FileSystemObjectType { get; set; }
public int Id { get; set; }
public object someUrl1 { get; set; }
public string SomeUrl2 { get; set; }
public string TypeId { get; set; }
public string DifferentValue1 { get; set; }
public bool DifferentValue2 { get; set; }
public List<string> DifferentValue3 { get; set; }
public List<string> DifferentValue4 { get; set;
属性DifferentValue1,DifferentValue2,DifferentValue3 ..是每个调查可能不同的问题。目前这项调查有4个问题,但其他调查可能会有不同的问题。其余的属性在整个调查中保持不变。
EG。其他调查可以有模型
public string value1 { get; set; }
public string value2 { get; set; }
public string value3 { get; set; }
public string value4 { get; set; }
public int FileSystemObjectType { get; set; }
public int Id { get; set; }
public object someUrl1 { get; set; }
public string SomeUrl2 { get; set; }
public string TypeId { get; set; }
public string DifferentValue1 { get; set; }
public bool DifferentValue2 { get; set; }
有没有办法可以创建一个通用模型类,其中问题的属性可以不同但其他属性是相同的?
感谢任何帮助。
答案 0 :(得分:0)
听起来像是一个典型的继承方案..
创建一个基类,其中包含您想要用于所有调查的属性,然后从该类派生,并将各个问题的属性添加到该子类。
示例:
public class SurveyBase
{
public int Id { get; set; }
public string SomeUrl { get; set; }
public string TypeId { get; set; }
//...
}
public class Survey1 : SurveyBase
{
public string MyQuestion1 { get; set; }
public string MyQuestion2 { get; set; }
//..
}
public class Survey2 : SurveyBase
{
public string OtherProperty { get; set; }
public int Whatever { get; set; }
//..
}