chcę zrobić coś takiego:Możliwy jest typ nullotyczny jako parametr ogólny?
myYear = record.GetValueOrNull<int?>("myYear"),
zauważy pustych typu jako parametru rodzajowego.
Ponieważ funkcja GetValueOrNull
mógł wrócić zerowy moja pierwsza próba była to:
public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)
where T : class
{
object columnValue = reader[columnName];
if (!(columnValue is DBNull))
{
return (T)columnValue;
}
return null;
}
ale błąd Dostaję teraz jest:
The type 'int?' must be a reference type in order to use it as parameter 'T' in the generic type or method
Right! Nullable<int>
to struct
! Dlatego starałem zmianę ograniczenia klasy do struct
przymusu (i jako efekt uboczny nie może wrócić null
więcej):
public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)
where T : struct
Teraz zadanie:
myYear = record.GetValueOrNull<int?>("myYear");
daje następujący błąd:
The type 'int?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method
Czy jest możliwe określenie typu zerowego jako parametru generycznego?
pls pls zrobić swój podpis 'IDataRecord' od' DbDataRecord' .. – nawfal