【发布时间】:2014-12-09 10:39:19
【问题描述】:
我在 object val = method.Invoke 行中的代码有问题。 我们使用此代码将 mdx 字段映射到我们的实体(集合)。一些数据具有 DBNull 值。所以我做了一项研究来检查 propertyType 是否为 Nullable 并实现了解决方案。请参阅 Nullable.GetUnderlyingType。但我仍然遇到这个错误。 无法将 DBNull.Value 转换为类型“System.Double”。请使用可为空的类型。
string propertyKey = entry.Key;
PropertyInfo property = entry.Value;
Type propertyType = property.PropertyType;
propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
object objectNeedingProperty = objectToPopulate;
MethodInfo method = _dataRowExtFieldMethod.MakeGenericMethod(new Type[] { propertyType });
object val = method.Invoke(row, new object[] { row, propertyKey });
property.SetValue(objectNeedingProperty, val, null);
【问题讨论】:
-
检查该值是否为DBNull,然后根据您的业务逻辑进行适当的处理。
-
使用“双倍?”这是可以为空的双精度,这意味着它接受空值。
-
老实说,我不知道在我上面的代码中放在哪里
标签: c# .net reflection