Attempt to access the method failed: System.Collections.Generic.List`1..ctor()

谁说我不能喝 提交于 2019-12-13 02:33:52

问题


I've this code through which I am retrieveing json data from my Localhost.But it is giving the error mentioned in my title.When I hover over the response while debugging.It shows me the correct response.I am using JSON.NET to parse json response.

var response = reader.ReadToEnd();

                   List<Company> cLst = JsonConvert.DeserializeObject<List<Company>>(response);    //Error

                    this.Dispatcher.BeginInvoke(() =>
                        {

                            foreach (Company c in cLst)
                            {
                                ListBoxItemControl Li = new ListBoxItemControl();
                                Li.CompanyNameTextBlock.Text = c.CompanyName;
                                Li.PromotionTextBlock.Text = c.PromotionText;
                                listBox1.Items.Add(Li);
                            }
                        });

Here is the Company Class.

class Company
{
    public string CompanyName {get; set;}
    public string CompanySlogan { get; set; }
    public string CompanyDescription { get; set; }
    public string CompanyRating { get; set; }
    public string CompanyDpPath { get; set; }
    public string CompanyOtherInfo { get; set; }
    public string CompanyFollowers { get; set; }
    public int CompanyID { get; set; }

    public int PromotionID { get; set; }
    public string PromotionText { get; set; }
    public string PromotionRating { get; set; }
    public string PromotionPicPath { get; set; }
    public string PromotionTitle { get; set; }
    public int PromotionLikes { get; set; }
    public int PromotionComments { get; set; }



}

回答1:


Take another class like,

 public class RootObject
 {
  public List<Company> companies;
 }

And then modify your code like this,

var jsonData = JsonConvert.DeserializeObject<RootObject>(response);
List<Company> cLst = jsonData.companies;

Try this and let me know.




回答2:


Try making the Company class public



来源:https://stackoverflow.com/questions/10783327/attempt-to-access-the-method-failed-system-collections-generic-list1-ctor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!