title: Generic method to read data from DataReader
body: <p>I am currently using this method to read data from DataReader - </p>
  
  <pre><code>private T GetValue&lt;T&gt;(object obj)
  {
      if (typeof(DBNull) != obj.GetType())
      {
          return (T)obj;
      }
      return default(T);
  }
  </code></pre>
  
  <p>calling above method as -  </p>
  
  <pre><code>GetValue&lt;int&gt;(dataReader["columnName1"])
  GetValue&lt;string&gt;(dataReader["columnName2"])
  GetValue&lt;float&gt;(dataReader["columnName3"])
  </code></pre>
  
  <p>However this fails when <code>columnName3</code> is having values as <code>7200000</code> with error<br>
  <code>Invalid Cast Exception.</code></p>
  
  <p>I am thinking to modify my method to replace -  </p>
  
  <pre><code>return (T)obj;
  </code></pre>
  
  <p>with</p>
  
  <pre><code>return (T)Convert.ChangeType(obj, typeof(T));
  </code></pre>
  
  <p>But looking forward for a better way as this change will involve type casting two times.<br>
  Any better ideas?</p>
  
  <p>Thank you!</p>