E
Earl Teigrob
I have a class that has only public fields of only SqlDataType types that
acts as a row(record) for my dataset table.
This "row" class is passed to one of my XML classes as a constructor and the
different methods in the XML class use its fields in the row class to
perform actions.
Several of my XML class methods use reflection to iterate through the fields
in the "row" class and determine whether each field value is null or not. I
have a way of performing this test but it seems very clumsy and I was
wondering of someone knew a more elegant(and efficient) way of accomplishing
the same task?
Thanks for your help!
Earl
Here is the code I am currently using...
//using reflection to iterate files of "row" class
foreach (System.Reflection.FieldInfo myFieldInfo in
DataRecord.GetType().GetFields())
{
object x = myFieldInfo.GetValue(DataRecord);
if (SqlTypeIsNull(x))
v[RowIndex].Row[myFieldInfo.Name]=x;
}
//ugly test to determine if SqlType is null
private bool SqlTypeIsNull(object o)
{
string ValueType=o.GetType().ToString();
switch (ValueType)
{
case "System.Data.SqlTypes.SqlBinary":
if (((System.Data.SqlTypes.SqlBinary)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlBoolean":
if (((System.Data.SqlTypes.SqlBoolean)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlByte":
if (((System.Data.SqlTypes.SqlByte)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDateTime":
if (((System.Data.SqlTypes.SqlDateTime)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDecimal":
if (((System.Data.SqlTypes.SqlDecimal)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDouble":
if (((System.Data.SqlTypes.SqlDouble)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt16":
if (((System.Data.SqlTypes.SqlInt16)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt32":
if (((System.Data.SqlTypes.SqlInt32)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt64":
if (((System.Data.SqlTypes.SqlInt64)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlMoney":
if (((System.Data.SqlTypes.SqlMoney)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlSingle":
if (((System.Data.SqlTypes.SqlSingle)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlString":
if (((System.Data.SqlTypes.SqlString)o).IsNull)
return true;
break;
default:
return true;
}
return false;
}
//this is the class that represents my datatable row and is used to build
the dataset
public class XmlDataRecord_Test
{
public System.Data.SqlTypes.SqlInt32 MySqlInt1;
public System.Data.SqlTypes.SqlInt32 MySqlInt2;
public System.Data.SqlTypes.SqlDateTime MySqlDateTime1;
}
acts as a row(record) for my dataset table.
This "row" class is passed to one of my XML classes as a constructor and the
different methods in the XML class use its fields in the row class to
perform actions.
Several of my XML class methods use reflection to iterate through the fields
in the "row" class and determine whether each field value is null or not. I
have a way of performing this test but it seems very clumsy and I was
wondering of someone knew a more elegant(and efficient) way of accomplishing
the same task?
Thanks for your help!
Earl
Here is the code I am currently using...
//using reflection to iterate files of "row" class
foreach (System.Reflection.FieldInfo myFieldInfo in
DataRecord.GetType().GetFields())
{
object x = myFieldInfo.GetValue(DataRecord);
if (SqlTypeIsNull(x))
v[RowIndex].Row[myFieldInfo.Name]=x;
}
//ugly test to determine if SqlType is null
private bool SqlTypeIsNull(object o)
{
string ValueType=o.GetType().ToString();
switch (ValueType)
{
case "System.Data.SqlTypes.SqlBinary":
if (((System.Data.SqlTypes.SqlBinary)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlBoolean":
if (((System.Data.SqlTypes.SqlBoolean)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlByte":
if (((System.Data.SqlTypes.SqlByte)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDateTime":
if (((System.Data.SqlTypes.SqlDateTime)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDecimal":
if (((System.Data.SqlTypes.SqlDecimal)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlDouble":
if (((System.Data.SqlTypes.SqlDouble)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt16":
if (((System.Data.SqlTypes.SqlInt16)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt32":
if (((System.Data.SqlTypes.SqlInt32)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlInt64":
if (((System.Data.SqlTypes.SqlInt64)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlMoney":
if (((System.Data.SqlTypes.SqlMoney)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlSingle":
if (((System.Data.SqlTypes.SqlSingle)o).IsNull)
return true;
break;
case "System.Data.SqlTypes.SqlString":
if (((System.Data.SqlTypes.SqlString)o).IsNull)
return true;
break;
default:
return true;
}
return false;
}
//this is the class that represents my datatable row and is used to build
the dataset
public class XmlDataRecord_Test
{
public System.Data.SqlTypes.SqlInt32 MySqlInt1;
public System.Data.SqlTypes.SqlInt32 MySqlInt2;
public System.Data.SqlTypes.SqlDateTime MySqlDateTime1;
}