Since the indexer of DataRow returns a System.Object, your best bet is something like the following:
public static class Extensions
{
public static bool DbBoolNullable(this object o)
{
return Convert.IsDBNull(o) ? false : bool.Parse(o.ToString());
}
}
However, I'd strongly advise against this, as since this is an extension method for System.Object
(which every class in .NET inherits from), then this method will apply to every variable.
A better way would probably to make an extension method for DataRow
:
public static bool GetBool(this DataRow row, string column)
{
return Convert.IsDbNull(row[column]) ? false : bool.Parse(row[column].ToString());
}
Then you could use it like so:
bool val = table.Rows[0].GetBool("ColumnName");