title: Get Collections from object using reflection and getCount (.net 4)
body: <p>I have a requirement to reflect on a object get all properties that are collections and</p>
  
  <p>1)GetCount for each collection </p>
  
  <p>2)GetTotalCount (allCollectionCount)</p>
  
  <p>3)Call a method with this collection.</p>
  
  <p>Below is what I have done so far with a made up noddy structure for semplicity.
      I am stuck in how to call this method and how to get count for collection.</p>
  
  <p>Any suggestions?</p>
  
  <pre><code>        using System;
      using System.Collections;
      using System.Collections.Generic;
      using System.Linq;
      using System.Reflection;
      using System.Text;
  
      namespace ConsoleApplication2
      {
          class Program
          {
              static void Main()
              {
                  var request = GetDataRequest();
  
                  //Get all properties 
                  List&lt;PropertyInfo&gt; propInfoList =
                      new List&lt;PropertyInfo&gt;(request.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public));
  
                  //Get collections only
                  var myClassCollections=propInfoList.Where(xxx =&gt; xxx.PropertyType.GetInterfaces().Any(x =&gt; x == typeof (IEnumerable))).ToList();
  
                  var totalCountForAllCollections=????
                  foreach (var col in myClassCollections)
                  {
                      //How do I call my Method DoSomething
                      //      DoSomething&lt;?&gt;(col.?????)   
                  }
  
              }
  
              public void DoSomething&lt;T&gt;(List&lt;T&gt; objectCollection)
              {
                  //etc...
              }
              private static DataRequest GetDataRequest()
              {
                  DataRequest request = new DataRequest();
                  request.Addresses.Add(new Address
                  {
                      Id = 1,
                      City = "London",
                      Postcode = "32131",
                      Street = "London Road"
                  });
                  request.Addresses.Add(new Address
                  {
                      Id = 2,
                      City = "NewYork",
                      Postcode = "3432",
                      Street = "NewYork Road"
                  });
  
                  request.Customers.Add(new Customer
                  {
                      Id = 1,
                      Name = "Jo",
                      Surname = "Bloggs",
                  });
                  request.Customers.Add(new Customer
                  {
                      Id = 1,
                      Name = "Jon",
                      Surname = "Bloggs2",
                  });
                  request.Customers.Add(new Customer
                  {
                      Id = 1,
                      Name = "Jonny",
                      Surname = "Bloggs3",
                  });
                  return request;
              }
          }
          public class DataRequest
          {
              public DataRequest()
              {
                  Customers = new List&lt;Customer&gt;();
                  Orders = new List&lt;Order&gt;();
                  Addresses = new List&lt;Address&gt;();
  
              }
              public List&lt;Customer&gt; Customers { get; set; }
              public List&lt;Order&gt; Orders { get; set; }
              public List&lt;Address&gt; Addresses { get; set; }
  
          }
  
          public class Customer
          {
              public int Id { get; set; }
              public string Name { get; set; }
              public string Surname { get; set; }
          }
  
          public class Order
          {
              public int Id { get; set; }
              public string Name { get; set; }
              public string OrderNo { get; set; }
          }
  
          public class Address
          {
              public int Id { get; set; }
              public string Street { get; set; }
              public string City { get; set; }
              public string Postcode { get; set; }
          }
      }
  </code></pre>