title: Need to find the nested dto object's property
body: <p>I have the following DTO with dummy data..I wanted to find out the count of Entries list from the StandardDTO object:-</p>
  
  <pre><code>public class StandardDTO
  {
      public string InternalNotes { get; set; }
      public string CustomerNotes { get; set; }
      public List&lt;Principal&gt; Principals { get; set; }
      public VerificationSummary VerificationSummary{ get; set; }
  }
  
  public class Principal
  {
      public string PrincipalTitle { get; set; }
      public string PrincipalName { get; set; }
  }
  
  public class VerificationSummary
  {
      public List&lt;Entry&gt; Entries { get; set; }
      public decimal GrossTotal { get; set; }
      public decimal Total { get; set; }
  }
  
  public class Entry
  {
      public string PeriodName { get; set; }
      public decimal Amount { get; set; }
  }
  
  void Main()
  {
      // Need to populate stdDTOObject and childXElement
      int count = GetDTOObjectCount(GetStandardDTOObject(), "VerificationSummary");
      count.Dump();
  }
  
  public StandardDTO GetStandardDTOObject()
  {
      StandardDTO stdDTOObj = new StandardDTO();
      stdDTOObj.InternalNotes = "InternalNotes";
      stdDTOObj.CustomerNotes = "CustomerNotes";
  
      List&lt;Principal&gt; lstPrincipal = new List&lt;Principal&gt;();
      Principal pObj = new Principal();
      pObj.PrincipalTitle = "Mr";
      pObj.PrincipalName = "ABC";
      lstPrincipal.Add(pObj);
  
      pObj = new Principal();
      pObj.PrincipalTitle = "Mrs";
      pObj.PrincipalName = "XYZ";
      lstPrincipal.Add(pObj);
  
      stdDTOObj.Principals = lstPrincipal;
  
      VerificationSummary vs = new VerificationSummary();
      List&lt;Entry&gt; lstEntry = new List&lt;Entry&gt;();
      Entry entry = new Entry();
      entry.PeriodName = "Sept17";
      entry.Amount = 1212;
      lstEntry.Add(entry);
  
      entry = new Entry();
      entry.PeriodName = "Oct17";
      entry.Amount = 12000;
      lstEntry.Add(entry);
  
      entry = new Entry();
      entry.PeriodName = "Nov17";
      entry.Amount = 1000;
      lstEntry.Add(entry);
  
      entry = new Entry();
      entry.PeriodName = "Dec17";
      entry.Amount = 2000;
      lstEntry.Add(entry);
  
      entry = new Entry();
      entry.PeriodName = "Jan18";
      entry.Amount = 2000;
      lstEntry.Add(entry);
  
      vs.Entries = lstEntry;
      vs.GrossTotal = 5555;
      vs.Total = 10000;
      stdDTOObj.VerificationSummary = vs;
      return stdDTOObj;
  }
  
  public int GetDTOObjectCount&lt;T&gt;(T dtoObject, string nodeName)
  {
      var dtoObjectType = dtoObject.GetType();
      var objectProperties = GetPropertyInfo(dtoObjectType);
  
      return GetDTOOBjectCountRecursively(objectProperties, nodeName, dtoObject);
  }
  
  public int GetDTOOBjectCountRecursively&lt;T&gt;(IEnumerable&lt;PropertyInfo&gt; objectProperties, string nodeName, T dtoObject)
  {
      foreach (PropertyInfo propInfo in objectProperties)
      {
          if (propInfo.Name.Equals(nodeName, StringComparison.OrdinalIgnoreCase))
          {
              var lstDTOItems = propInfo.GetValue(dtoObject) as IList;
  
              if (lstDTOItems != null)
              {
                  return lstDTOItems.Count;
              }
              else
              {
                  var objPropInfos = GetPropertyInfo(propInfo.PropertyType);
                  //hardcoded the nodeName just for this test.
                  return GetDTOOBjectCountRecursively(objPropInfos, "Entries", dtoObject);
              }
          }
      }
      return 0;
  }
  
  private IEnumerable&lt;PropertyInfo&gt; GetPropertyInfo(Type type)
  {
      return type.GetProperties();
  }
  </code></pre>
  
  <p>Issue is that I'm unable to loop recursively inside 
  StandardDTO -> VerificationSummary -> Entries</p>
  
  <p>It fails at the following line when propinfo = "Entries" propinfo</p>
  
  <p>var lstDTOItems = propInfo.GetValue(dtoObject) as IList;</p>