I'm parsing an XML file with the XmlReader
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:
private static T ReadData<T>(XmlReader reader, string value)
{
reader.MoveToAttribute(value);
object readData = reader.ReadContentAsObject();
return (T)readData;
}
As I came to realise, this does not work entirely as I have planned; it throws an error with primitive types such as int
or double
, since a cast cannot convert from a string
to a numeric type. Is there any way for my function to prevail in modified form?
First check to see if it can be cast.
if (readData is T) {
return (T)readData;
}
try {
return (T)Convert.ChangeType(readData, typeof(T));
}
catch (InvalidCastException) {
return default(T);
}