title: Count objects within dynamic anonymous object (C#)
body: <p>I have a dynamic object (it's actually json) that I pass into my MVC WebApi controller. </p>
  
  <p>The json object contains multiple lists within an anonymous object that are submitted to the controller from another application via <code>client.PostAsJsonAsync("myapiurl", objectGraph)</code>.</p>
  
  <p>What I need to do to validate the object on the MVC side, is to get the count of objects in each list. I can access the lists dynamically via <code>mydynamicobject.mylist</code> and individual items via <code>mydynamicobject.mylist[index]</code> but I can't seem to be able to get a count of <code>mydynamicobject.mylist</code>. </p>
  
  <p>What I've tried so far:</p>
  
  <ul>
  <li>LINQ extension methods - doesn't work on dynamic</li>
  <li><code>Enumerable.Count(mydynamicobject.mylist)</code> - can't infer type</li>
  </ul>
  
  <p>Any other ideas? The count is actually correctly available in the dynamic object's <code>base</code> but obviously not accessible as a property. Help!</p>
  
  <p>This works now:</p>
  
  <pre><code>// This is a MVC/WebApi method
  public dynamic Post(dynamic mydynamicobject)
  
  if (((ICollection)mydynamicobject.mylist).Count == 0)
  {
  // do something
  }
  </code></pre>
  
  <p>The code that sends the dynamic object (different app):</p>
  
  <pre><code>HttpClient client = new HttpClient();  
  client.DefaultRequestHeaders.Accept.Add
    (new MediaTypeWithQualityHeaderValue("application/json")); 
  
  var objectGraph = new { mylist = new { Id = 1 }, mylist2 = new { Name = "ABC" } }; 
  var r = client.PostAsJsonAsync("api/mycontroller", objectGraph).Result;
  </code></pre>