如何检测与 nullable 属性 结合的数据类型
How to detect data type combined with nullable property
我正在使用反射来检索通用 class 对象 属性,方法是检测它们的数据类型(例如 System.String、System.DateTime 等)并根据以下内容转换值数据类型,例如:
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;
}
但是,当我遇到属性定义为int?,double?或 DateTime? 这将是一个 Nullable 类型,我无法弄清楚 属性 的确切数据类型,t 他的反射只给我类型 "System.Nullable" ,有没有办法检测后面的组合数据类型?
如 mentioned on comment section. You need to use Nullable.GetUnderlyingType
如下:
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;
}
我正在使用反射来检索通用 class 对象 属性,方法是检测它们的数据类型(例如 System.String、System.DateTime 等)并根据以下内容转换值数据类型,例如:
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;
}
但是,当我遇到属性定义为int?,double?或 DateTime? 这将是一个 Nullable 类型,我无法弄清楚 属性 的确切数据类型,t 他的反射只给我类型 "System.Nullable" ,有没有办法检测后面的组合数据类型?
如Nullable.GetUnderlyingType
如下:
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;
}