c# - generic field getter for a DataRow -
i try extend datarow object generic method :
public static t? get<t>(this datarow row, string field) t : struct { if (row.isnull(field)) return default(t); else return (t)row[field]; }
it's work fine when t int
, decimal
, double
, etc.
but when try use string, have error :
"the type 'string' must non-nullable value type in order use parameter 't' in generic type or method 'system.nullable'"
how can correct ?
i know string not struct wan't return null if string field dbnull.
i think want:
public static t? getvalue<t>(this datarow row, string field) t : struct { if (row.isnull(field)) return new t?(); else return (t?)row[field]; } public static t getreference<t>(this datarow row, string field) t : class { if (row.isnull(field)) return default(t); else return (t)row[field]; }
Comments
Post a Comment