Cannot cast DBNull.Value to type 'System.Doubl

2019-08-09 17:18发布

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);

1条回答
神经病院院长
2楼-- · 2019-08-09 17:27

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

查看更多
登录 后发表回答