【问题标题】:Cannot create and populate list type System.Collections.ICollection无法创建和填充列表类型 System.Collections.ICollection
【发布时间】:2021-12-19 21:27:35
【问题描述】:

确切的错误是Cannot create and populate list type System.Collections.ICollection. Path'[0].assocPosition.assocJobtitle.employees''

我有这个方法

public static ArrayList Get<T>(string url)
{
    using (var response = _httpClient.GetAsync(baseUrl + url).Result)
    {
        if (response.IsSuccessStatusCode)
        {
            var customerJsonString = response.Content.ReadAsStringAsync().Result;
            var list = JsonConvert.DeserializeObject<List<T>>(customerJsonString);
            var arrayList = new ArrayList(list.Count);
            foreach (T item in list)
            {
                arrayList.Add(item);
            }
            return arrayList;
        }
    }
    return null;
}

在放置断点后我发现问题在于对象没有正确反序列化。

public static ArrayList EmployeeAssignmentFinder_FindAll()
{
    return HttpCaller.Get<EmployeeAssignment>("WorkflowEngine/EmployeeAssignmentFinder_FindAll");
}

控制器内部的HttpGet:

[HttpGet("WorkflowEngine/EmployeeAssignmentFinder_FindAll")]
        public IActionResult WorkflowEngineEmployeeAssignmentFinder_FindAll()
{
    return Ok(_engine.VC.EmployeeAssignmentFinder_FindAll());
}

EmployeeAssignment 类有内部:

private Position m_Position;
public Position AssocPosition 
{
    get { return m_Position; }
    set { m_Position = value; }
}

职位类有内部:

private Jobtitle m_AssocJobtitle;
public Jobtitle AssocJobtitle 
{
    get { return m_AssocJobtitle; }
    set { m_AssocJobtitle = value; }
}

而 Jobtitle 类有里面:

private ICollection m_Employees;
public ICollection Employees
{
    get{ return m_Employees; }
    set { m_Employees = value;}
}

ICollectionArrayList 可能存在冲突。

【问题讨论】:

标签: c# arraylist json.net icollection


【解决方案1】:

尝试用 T 的 ICollection 替换 ICollection,因为最后一个被 EF 使用。

private ICollection<Employee> m_Employees
public ICollection<Employee> Employees
{
   get { return m_Employees; }
   set { m_Employees = value; }
}

恕我直言,您应该使用 List 而不是 ArrayList,因为它已经是 net 5+,而不是 net 1.O

public static List<T> Get<T>(string url)
{
    using (var response = _httpClient.GetAsync(baseUrl + url).Result)
    {
        if (response.IsSuccessStatusCode)
        {
            var customerJsonString = response.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<List<T>>(customerJsonString);
        
        }
    }
    return null;
}

public static List<EmployeeAssignment> EmployeeAssignmentFinder_FindAll()
{
   return HttpCaller.Get<EmployeeAssignment>("WorkflowEngine/EmployeeAssignmentFinder_FindAll");
}

【讨论】:

  • 我仍然遇到同样的错误,整个解决方案也取决于 ArrayList。
  • @Christos 修复整个解决方案。数组列表不能用,20年前就用过了。
  • 我相信这里的关键变化是使用ICollection&lt;T&gt; 的通用版本,因为standard one 没有Add() 方法,因此JSON.NET 无法实例化它。该问题与ArrayList 无关,您不必将其删除。 @Christos 您现在正在使用此接口的通用版本吗?还是不行?
  • 我试图用 ICollection 替换 ICollection,但现在程序根本不加载员工。我需要保留 ArrayList 即使它很旧,并且可能在 Get 方法中进行一些更改。
猜你喜欢
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 2017-10-31
  • 1970-01-01
相关资源
最近更新 更多