I have properties that wrap DataRow columns as in:
public int aNumber
{ get{ return m_DataRow["aColumnNam e"]; }
set{ m_DataRow["aColumnNam e"] = value; } }
If the column happens to contain DBNull, I get a cast
exception since DBNull cannot be converted to int. I
wrote the following method that looks up the column's
data type and if it is a ValueType, returns the default
value for the ValueType.
I want the method to use reflection to get the default
value, so I don't have to code for each data type. However,
GetConstructor always returns null. Is there some other
way to get the default value for a ValueType?
Thanks,
Brian Brane
Here's the method...
// Instance variables containing default values (work around)
bool m_Bool;
byte m_Byte;
DateTime m_DateTime;
Guid m_Guid;
int m_Int;
private object GetColumnValueO rDefault(DataRo w dataRow, string columnName)
{
DataColumn column = dataRow.Table.C olumns[columnName];
if (column == null)
throw new Exception("Inva lid column name: " + columnName);
// If the column contains a value, then return the value
object value = dataRow[column];
if (value != System.DBNull.V alue)
return value;
// If the column is not a ValueType, then return null
System.Type dataType = column.DataType ;
if (!dataType.IsVa lueType)
return null;
// Use reflection to create a new ValueType
System.Reflecti on.ConstructorI nfo constructor =
dataType.GetCon structor(System .Type.EmptyType s);
if (constructor != null) //// IS ALWAYS NULL!
{
object defaultValue = constructor.Inv oke(new object[0]);
return defaultValue;
}
// Return a ValueType that is initialized to its default value
if (dataType == typeof(System.B oolean))
return m_Bool;
if (dataType == typeof(System.B yte))
return m_Byte;
if (dataType == typeof(System.D ateTime))
return m_DateTime;
if (dataType == typeof(System.G uid))
return m_Guid;
if (dataType == typeof(System.I nt32))
return m_Int;
throw new Exception("Unab le to determine default for " + columnName);
}
public int aNumber
{ get{ return m_DataRow["aColumnNam e"]; }
set{ m_DataRow["aColumnNam e"] = value; } }
If the column happens to contain DBNull, I get a cast
exception since DBNull cannot be converted to int. I
wrote the following method that looks up the column's
data type and if it is a ValueType, returns the default
value for the ValueType.
I want the method to use reflection to get the default
value, so I don't have to code for each data type. However,
GetConstructor always returns null. Is there some other
way to get the default value for a ValueType?
Thanks,
Brian Brane
Here's the method...
// Instance variables containing default values (work around)
bool m_Bool;
byte m_Byte;
DateTime m_DateTime;
Guid m_Guid;
int m_Int;
private object GetColumnValueO rDefault(DataRo w dataRow, string columnName)
{
DataColumn column = dataRow.Table.C olumns[columnName];
if (column == null)
throw new Exception("Inva lid column name: " + columnName);
// If the column contains a value, then return the value
object value = dataRow[column];
if (value != System.DBNull.V alue)
return value;
// If the column is not a ValueType, then return null
System.Type dataType = column.DataType ;
if (!dataType.IsVa lueType)
return null;
// Use reflection to create a new ValueType
System.Reflecti on.ConstructorI nfo constructor =
dataType.GetCon structor(System .Type.EmptyType s);
if (constructor != null) //// IS ALWAYS NULL!
{
object defaultValue = constructor.Inv oke(new object[0]);
return defaultValue;
}
// Return a ValueType that is initialized to its default value
if (dataType == typeof(System.B oolean))
return m_Bool;
if (dataType == typeof(System.B yte))
return m_Byte;
if (dataType == typeof(System.D ateTime))
return m_DateTime;
if (dataType == typeof(System.G uid))
return m_Guid;
if (dataType == typeof(System.I nt32))
return m_Int;
throw new Exception("Unab le to determine default for " + columnName);
}
Comment