简体   繁体   中英

How to detect data type combined with nullable property

I am using reflection to retrieve generic class object property, by detecting their data type (eg System.String, System.DateTime, etc) and convert value based on the data type, eg:

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, t he reflection only gives me the type as "System.Nullable" , is there anyway to detect the combined data type behind?

As @madreflection mentioned on comment section. You need to use Nullable.GetUnderlyingType as following:

var propType = prop.PropertyType;

if (Nullable.GetUnderlyingType(propType) != null)
{
    // It's nullable
    propType = Nullable.GetUnderlyingType(propType);
}

switch (propType.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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM