C# 如何将列表填充为类对象?
如何将列表填充为类对象?例如,这不起作用:C# 如何将列表填充为类对象?,c#,list,C#,List,如何将列表填充为类对象?例如,这不起作用: [DataContract] public class JsonReviewFormFields { [DataMember] public PersonalDevelopmentPlan personalDevelopmentPlan { get; set; } } public class PersonalDevelopmentPlan { public List<ShortTerm> shortT
[DataContract]
public class JsonReviewFormFields
{
[DataMember]
public PersonalDevelopmentPlan personalDevelopmentPlan { get; set; }
}
public class PersonalDevelopmentPlan
{
public List<ShortTerm> shortTerm { get; set; }
public List<LongTerm> longTerm { get; set; }
}
public class ShortTerm
{
public string workRelated { get; set; }
public string structured { get; set; }
public string informal { get; set; }
public string reviewDate { get; set; }
}
public class LongTerm
{
public string workRelated { get; set; }
public string structured { get; set; }
public string informal { get; set; }
public string reviewDate { get; set; }
}
这可能是一个基本的面向对象的错误。如何填充列表?如果不是实例化列表,则必须先实例化类型:
jsonReviewFormFields.personalDevelopmentPlan = new PersonalDevelopmentPlan();
然后设置它的属性:
jsonReviewFormFields.personalDevelopmentPlan.shortTerm = _itemsShort
在此之前,您还必须实例化我在任何地方的控制器操作中都没有看到的主类:
JsonReviewFormFields jsonReviewFormFields = new JsonReviewFormFields();
是否可以添加崩溃日志或错误消息?定义了
jsonReviewFormFields
的位置在哪里?jsonReviewFormFields jsonReviewFormFields=new jsonReviewFormFields();Ehsan,忘记在上面的代码中包含。现在一切都好了,ta
jsonReviewFormFields.personalDevelopmentPlan.shortTerm = _itemsShort
JsonReviewFormFields jsonReviewFormFields = new JsonReviewFormFields();