Cannot create and populate list type System.Collections.ICollection

464 Views Asked by At

The exact error is Cannot create and populate list type System.Collections.ICollection. Path'[0].assocPosition.assocJobtitle.employees''

I have this method

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;
}

where after putting breaking point i see that the problem is that the object doesnt deserialize properly.

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

The HttpGet inside the controller:

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

The EmployeeAssignment class has inside:

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

The Position Class has inside:

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

And Jobtitle class has inside:

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

It might there is a conflict from ICollection to ArrayList.

1

There are 1 best solutions below

5
Serge On

Try to replace ICollection by ICollection of T since the last one is used by EF.

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

and IMHO you should use List instead of ArrayList, since it is net 5+ already, not 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");
}