title: How to efficiently create a list of objects inside of a generic method?
body: <p>So, I have an application which lies on a database. So far, the results of my queries all went into a DataTable object like this:</p>
  
  <pre><code>DataTable data = new DataTable();
  data.Load(someQuery.ExecuteReader());
  </code></pre>
  
  <p>Now, I want to load my data into a list of a strongly typed objects. Something like this:</p>
  
  <pre><code>List&lt;MyClass&gt; data = someQuery.Load&lt;MyClass&gt;();
  </code></pre>
  
  <p>However, my first take on writing that method ended up running almost three times slower than DataTable.Load(IDataReader) method. Basically, I have user GetConstructor(null).Invoke(null) to create and object and I have used PropertyInfo.SetValue(reader.GetValue()) to fill it with data. </p>
  
  <p>Is there a better way to do this?</p>
  
  <p>The method used:</p>
  
  <pre><code>    public List&lt;T&gt; LoadData&lt;T&gt;(DbCommand query)
      {
          Type t = typeof(T);
  
          List&lt;T&gt; list = new List&lt;T&gt;();
          using (IDataReader reader = query.ExecuteReader())
          {
              while (reader.Read())
              {
                  T newObject = (T)t.GetConstructor(null).Invoke(null);
  
                  for (int ct = 0; ct &lt; reader.FieldCount; ct++)
                  {
                      PropertyInfo prop = t.GetProperty(reader.GetName(ct));
                      if (prop != null)
                          prop.SetValue(newObject, reader.GetValue(ct), null);
                  }
  
                  list.Add(newObject);
              }
          }
  
          return list;
      }
  </code></pre>