Re: Passing property Accesor as Function`
Marc Brooks <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
> Richard, I'm idealistically trying to find a way of choosing the correct
> type conversion based on the type of the property. Like I said, it was
> really a hypothetical issue; I'm going to use PropertyInfo objects and
> attributes in production.
I would contend that most of your problem goes away if you realize
that your conversion needs to know what the default value is... thus
it needs to be passed to the converter, thus it supplies the correct
type information... something like this from my DAO helpers:
public static DateTime ColumnValue(IDataRecord reader, int
columnIndex, DateTime assumed)
{
if (reader == null)
throw new ArgumentNullException("reader");
object dbValue = reader.GetValue(columnIndex);
DateTime value = DBNull.Value.Equals(dbValue) ? assumed :
(DateTime)dbValue;
if (value == System.Data.SqlTypes.SqlDateTime.MinValue)
value = DateTime.MinValue;
if (value == System.Data.SqlTypes.SqlDateTime.MaxValue)
value = DateTime.MaxValue;
return value;
}
public static T ColumnValue<T>(IDataRecord reader, int
columnIndex, T assumed)
{
object dbValue = reader.GetValue(columnIndex);
T value = DBNull.Value.Equals(dbValue) ? assumed : (T)dbValue;
return value;
}
Note my special casing for DateTime is to handle sentinal dates in the
database being automatically mapped to DateTime.MinValue and
DateTime.MaxValue. If you don't have something like that going on,
you don't need ANY type-specific versions.
--
"He uses statistics as a drunken man uses lamp-posts… for support
rather than illumination." Andrew Lang
Marc C. Brooks
http://musingmarc.blogspot.com