c# - What the Exception? InvalidCastException -
in legacy application, have sqldatareader
. following field creates invalidcastexception
bit
field.
public static t getvalueornull<t>(this idatareader reader, string column) { int ordinal; if(!string.isnullorempty(column) && !reader.isdbnull(reader.getordinal(column))) if(int.tryparse(reader.getordinal(column).tostring(), out ordinal)) return (t)reader.getvalue(ordinal); return default(t); }
what odd, (t)reader.getvalue(ordinal)
assign valid type. assigns false
, error still occurs. i'm not entirely sure why.
if perform gettype();
indeed show boolean
type. i've checked ensure threadsafe.
the reason code fail on bit field, correlated boolean due inferred type. underlying data type wasn't bool
rather bool?
.
once model reflected nullable boolean, code worked intended.
Comments
Post a Comment