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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -