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