我正在使用反射通过检测其数据类型(例如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?的属性时,要加倍?或DateTime?这将是一个Nullable类型,我无法弄清楚该属性的确切数据类型,反射仅给我提供了类型为“ System.Nullable”的类型,总之有什么可以检测后面的组合数据类型吗?
参考方案
正如@madreflection在评论部分中提到的。您需要按以下方式使用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;
}
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
改造正在返回一个空的响应主体 - java我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…
每个文件合并后添加换行 - python我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…
Json到php,json_decode返回NULL - php我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…
这个json格式正确吗? - c#我尝试解析时有json数据,返回错误的语法错误,请帮助我发现语法错误。[{"isData":"Yes","Details":"[{"Id":"70","Name":"Test","FileName…