title: Populate DataTable dynamically using Object Properties
body: <p>I'm trying to add an objects properties to a datatable dymamically. I'm trying to stay away from explicitly stating the properties of the object since I want this code to work for every object in my application. 
   I've read the <a href="https://stackoverflow.com/questions/14477500/adding-object-to-datatable-and-create-a-dynamic-gridview">Adding object to DataTable and create a dynamic GridView</a> but this isn't dynamic enough for me since he is explicitly specifying the properties. </p>
  
  <pre><code>private static void Main(string[] args)
          {
              //Read in JSON from text file
              StreamReader fileStream = new StreamReader(@"C:\Users\Aid\Desktop\test.txt");
              string json = fileStream.ReadToEnd();
  
              //Deserialise and map to class
              Client u = JsonConvert.DeserializeObject&lt;Client&gt;(json);
  
              DataTable dt = new DataTable();
  
              List&lt;string&gt; CliProperties = new List&lt;string&gt;();
              //Loop through Object properties and add to Datatable columns
              CliProperties = objProps(u);
  
              DataRow newRow = dt.NewRow();
              foreach (var prop in CliProperties)
              {
                  dt.Columns.Add(prop);
                  newRow[prop] = u.prop;
              }
  
              Console.ReadKey();
          }
  
  
  
          /// &lt;summary&gt;
          /// Get list of object properties
          /// &lt;/summary&gt;
          /// &lt;param name="obj"&gt;&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          public static List&lt;string&gt; objProps(Object obj)
          {
              List&lt;string&gt; Props = new List&lt;string&gt;();
  
              Type objType = obj.GetType();
              foreach (PropertyInfo propertyInfo in objType.GetProperties())
              {
                  if (propertyInfo.CanRead)
                  {
                      Props.Add(propertyInfo.Name);
                  }
              }
              return Props;
          }
      }
  
      public static class DataColumnCollectionExtensions
      {
          public static IEnumerable&lt;DataColumn&gt; AsEnumerable(this DataColumnCollection source)
          {
              return source.Cast&lt;DataColumn&gt;();
          }
      }
  </code></pre>
  
  <p>The problem area for me is: </p>
  
  <pre><code>foreach (var prop in CliProperties)
              {
                  dt.Columns.Add(prop);
                  newRow[prop] = u.prop;
              }
  </code></pre>
  
  <p>I'm getting the error: <code>'Client' does not contain a definition for 'prop' and no extension method 'prop' accepting first argument of type 'Client'</code></p>
  
  <p>I understand why I'm getting the error however I'm unsure how to make it work. </p>
  
  <p>I'm trying to add a new Row with PropertyName <code>newRow[prop]</code> with the values of the objects property value <code>= u.prop</code>.</p>
  
  <p>EDIT: The DataTable Column names will be identical to the object property names. </p>
  
  <p>The first iteration of the foreach should be the following: </p>
  
  <pre><code>foreach (var prop in CliProperties)
  {
      dt.Columns.Add("RefID");
      newRow["RefID"] = u.RefID;
  }
  </code></pre>