title: Dynamically adding items to a List<T> through reflection
body: <p>Lets say I have this class</p>
  
  <pre><code>class Child {
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  
  class Container {
      public List&lt;Child&gt; { get; set; }
  }
  </code></pre>
  
  <p>I'm working on a deserializer of sorts and I want to be able to create and populate the <code>Child</code> list from the data retrieved. I've gotten this far (I've cut out a lot of handling for other types for this example so its unnecessarily "iffy" but bear with me):</p>
  
  <pre><code>var props = typeof(Container).GetProperties();
  
  foreach (var prop in props) {
      var type = prop.PropertyType;
      var name = prop.Name;
  
      if (type.IsGenericType) {
          var t = type.GetGenericArguments()[0];
  
          if (type == typeof(List&lt;&gt;)) {
              var list = Activator.CreateInstance(type);
              var elements = GetElements();
  
              foreach (var element in elements) {
                  var item = Activator.CreateInstance(t);
                  Map(item, element); 
  
                  // ??? how do I do list.Add(item) here?
              }
  
              prop.SetValue(x, list, null); // x is an instance of Container
  
          }
      }
  }
  </code></pre>
  
  <p>I can't figure out how to cast <code>list</code> to essentially <code>List&lt;t.GetType()&gt;</code> so I can access the add method and add the item.</p>