I am using reflection to retrieve generic class object property, by detecting their data type (e.g System.String, System.DateTime, etc) and convert value based on the data type, e.g:
switch (prop.PropertyType.FullName)
{
case "System.String":
prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
string.Empty : Convert.ToString(_propertyDataValue));
break;
case "System.Int32":
prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
-1 : Convert.ToInt32(_propertyDataValue));
break;
case "System.DateTime":
prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
DateTime.MinValue : Convert.ToDateTime(_propertyDataValue));
break;
case "System.Double":
prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
0 : Convert.ToDouble(_propertyDataValue));
break;
case "System.Boolean":
prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
false : Convert.ToBoolean(_propertyDataValue));
break;
default:
prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
string.Empty : Convert.ToString(_propertyDataValue));
break;
}
However, when I met the property defined as int?, double? or DateTime? which would be a Nullable type, I cannot figure out which exact data type for the property, the reflection only gives me the type as "System.Nullable", is there anyway to detect the combined data type behind?
Aucun commentaire:
Enregistrer un commentaire