我的对象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);

最佳答案

您的对象具有类型double的属性。将其更改为double?,以便可以为该属性分配空值。

关于c# - 无法将DBNull.Value强制转换为类型“System.Double”。请使用可为空的类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27376883/

10-10 04:04