title: Invalid cast from 'System.String' to 'System.TimeSpan'
body: <p>I currently have a Generic method that reads an Value from the database based on a key and returns a specific type of that value.</p>
  
  <pre><code>    public T Get&lt;T&gt;(string key, T defaultValue)
      {
          var myparms = new List&lt;SqlParameter&gt;
                        {
                            new SqlParameter("@KEY", key),
                        };
          const string getPropertyQuery = "SELECT SPARM_VALUE FROM SYSPARAMS WHERE SPARM_KEY = @KEY;";
  
          var returnedValue = //Get value from Database
  
          if (returnedValue == null)
          {
              return defaultValue; //Value does not exists so return default.
          }
  
          return (T)Convert.ChangeType(returnedValue, typeof(T));
      }
  </code></pre>
  
  <p>But when I try and return a TimeSpan type I get the following exception. <code>Invalid cast from 'System.String' to 'System.TimeSpan'.</code></p>
  
  <p>After a bit of googling I found that the most common solution is to use the <code>TimeSpan.Parse</code> or <code>TimeSpan.TryParse</code> methods. </p>
  
  <p>I also found <a href="http://msdn.microsoft.com/en-us/library/system.timespan.aspx" rel="noreferrer">TimeSpan</a> Structure.</p>
  
  <p><code>public struct TimeSpan : IComparable, IComparable, 
      IEquatable, IFormattable</code></p>
  
  <p>My question is why, why is TimeSpan not able to do this. Is it because it doesn't have the <code>IConvertible</code> interface? Any thoughts on this would be much appreciated.</p>