In my previous blog, I wrote about how to create generic utility classes that would cast or convert any object to a given type, returning a default if the value was DBNull. I quickly discovered that this code will work just fine as long as the passed type is not nullable. For example, this will work:
int i = ConvertDBNull.To<int>( value, 0 );
But this won't:
int? i = ConvertDBNull.To<int?>( value, null );
The line that fails within the To<>() method is this:
return (T) Convert.ChangeType( value, t );
The reason the call to ChangeType() ...