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);
Your object has property of type double. Change it to double? so you can assign nulls to that property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With