I'm trying to write a generic method to read data from SQLDataReader. It works really well except when I want to get custom default value for some data types. For example, for string I want to get string.Empty instead of null.
public static T SafeGetValue<T>(SqlDataReader dr, string columnName)
{
T returnValue = default(T);
var value = dr[columnName];
if (value != null && value != DBNull.Value)
{
returnValue = (T)value;
}
else
{
returnValue.Null();
}
return returnValue;
}
public static object Null(this object o)
{
return null;
}
public static string Null(this string stringValue)
{
return string.Empty;
}
When T is string, I'm trying to get it to goto Null overload of string but it still goes to the object overload. Is there any way to do this?
T defaultValueas a parameter