无法将 DBNull.Value 转换为类型“System.Double".请使用可空类型 [英] Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type

查看:113
本文介绍了无法将 DBNull.Value 转换为类型“System.Double".请使用可空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在object val = method.Invoke 行 中的代码有问题.我们使用此代码将 mdx 字段映射到我们的实体(集合).某些数据具有 DBNull 值.所以我做了一个研究来检查 propertyType 是否为 Nullable 并实现了解决方案.请参阅Nullable.GetUnderlyingType.但我仍然遇到这个错误.无法将 DBNull.Value 转换为类型System.Double".请使用可为空类型.

I'm having a problem with my code in object val = method.Invoke line. We're using this code to map the mdx fields to our entities (collection). Some data has DBNull values. So I did a research to check if the propertyType is Nullable and implemented the solution. see Nullable.GetUnderlyingType. But I'm still encountering this error. Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type.

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? 以便您可以为该属性分配空值.

Your object has property of type double. Change it to double? so you can assign nulls to that property.

这篇关于无法将 DBNull.Value 转换为类型“System.Double".请使用可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆