title: DataRow Field Cast From Variable
body: <p>Is it possible to cast a type from a variable?  I'm extracting data from a spreadsheet into a class, but because some columns are strings, and others DateTime, I really want a do-all command, that I don't need to map everything manually.</p>
  
  <p>What I have so far:</p>
  
  <pre><code>foreach (PropertyInfo pinfo in asset.GetType().GetProperties())
  {
      string columnType = (from DataColumn column in data.Columns where column.ColumnName == pinfo.Name select column).First().DataType.ToString();
      pinfo.SetValue(asset, row.Field&lt;columnType&gt;(pinfo.Name));
  }
  </code></pre>
  
  <p>At the moment, it doesn't like <code>row.Field&lt;columnType&gt;()</code>, because it's not a real type.</p>
  
  <p>Is it possible to do something like the above, where I'm getting the type contained in a column, and casting this to retrieve the data for that column?  I'm in this situation, as I want to retrieve anything using the following statement, regardless of whether it's a string, int or DateTime.</p>
  
  <pre><code>var foo = row.Field&lt;string&gt;("Column Name");
  </code></pre>
  
  <p>Is there any generic command I can use?  Thanks</p>