B
Brian Brane
I have properties that wrap DataRow columns as in:
public int aNumber
{ get{ return m_DataRow["aColumnName"]; }
set{ m_DataRow["aColumnName"] = 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 GetColumnValueOrDefault(DataRow dataRow, string columnName)
{
DataColumn column = dataRow.Table.Columns[columnName];
if (column == null)
throw new Exception("Invalid column name: " + columnName);
// If the column contains a value, then return the value
object value = dataRow[column];
if (value != System.DBNull.Value)
return value;
// If the column is not a ValueType, then return null
System.Type dataType = column.DataType;
if (!dataType.IsValueType)
return null;
// Use reflection to create a new ValueType
System.Reflection.ConstructorInfo constructor =
dataType.GetConstructor(System.Type.EmptyTypes);
if (constructor != null) //// IS ALWAYS NULL!
{
object defaultValue = constructor.Invoke(new object[0]);
return defaultValue;
}
// Return a ValueType that is initialized to its default value
if (dataType == typeof(System.Boolean))
return m_Bool;
if (dataType == typeof(System.Byte))
return m_Byte;
if (dataType == typeof(System.DateTime))
return m_DateTime;
if (dataType == typeof(System.Guid))
return m_Guid;
if (dataType == typeof(System.Int32))
return m_Int;
throw new Exception("Unable to determine default for " + columnName);
}
public int aNumber
{ get{ return m_DataRow["aColumnName"]; }
set{ m_DataRow["aColumnName"] = 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 GetColumnValueOrDefault(DataRow dataRow, string columnName)
{
DataColumn column = dataRow.Table.Columns[columnName];
if (column == null)
throw new Exception("Invalid column name: " + columnName);
// If the column contains a value, then return the value
object value = dataRow[column];
if (value != System.DBNull.Value)
return value;
// If the column is not a ValueType, then return null
System.Type dataType = column.DataType;
if (!dataType.IsValueType)
return null;
// Use reflection to create a new ValueType
System.Reflection.ConstructorInfo constructor =
dataType.GetConstructor(System.Type.EmptyTypes);
if (constructor != null) //// IS ALWAYS NULL!
{
object defaultValue = constructor.Invoke(new object[0]);
return defaultValue;
}
// Return a ValueType that is initialized to its default value
if (dataType == typeof(System.Boolean))
return m_Bool;
if (dataType == typeof(System.Byte))
return m_Byte;
if (dataType == typeof(System.DateTime))
return m_DateTime;
if (dataType == typeof(System.Guid))
return m_Guid;
if (dataType == typeof(System.Int32))
return m_Int;
throw new Exception("Unable to determine default for " + columnName);
}