title: Cast object to T
body: <p>I'm parsing an XML file with the <code>XmlReader</code> class in .NET and I thought it would be smart to write a generic parse function to read different attributes generically. I came up with the following function:</p>
  
  <pre><code>private static T ReadData&lt;T&gt;(XmlReader reader, string value)
  {
      reader.MoveToAttribute(value);
      object readData = reader.ReadContentAsObject();
      return (T)readData;
  }
  </code></pre>
  
  <p>As I came to realise, this does not work entirely as I have planned; it throws an error with primitive types such as <code>int</code> or <code>double</code>, since a cast cannot convert from a <code>string</code> to a numeric type. Is there any way for my function to prevail in modified form?</p>