0

In the next code, I need to count the number of items on the generic list, which is the property in another generic class. I don't know which value should I supply to GetValue() method. Any ideas?

// This is just an example class - there can be another class with a different name for DetailsList
public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Bar> DetailsList { get; set; }
}

int NumberOfItems(TItem argsItem)
{
    var detailsList = (IList) argsItem.GetType().GetProperty("DetailsList").GetValue(?);  // how do I get a value of list?
    return detailsList.Count;
}

s

Elshad Shabanov
  • 483
  • 10
  • 17
  • 2
    There's nothing special about this being a list - the argument to `GetValue` is the "target" that you want to evaluate the property against... in this case `argsItem`. – Jon Skeet Jun 06 '22 at 14:38
  • Yes, you are right. I overthought it. I was thinking that the target must be the instance of the list. – Elshad Shabanov Jun 06 '22 at 14:50

0 Answers0