Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将列表填充为类对象?_C#_List - Fatal编程技术网

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();