title: Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type
body: <p>I'm having a problem with my code in <strong>object val = method.Invoke line</strong>.
  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 <strong>Nullable.GetUnderlyingType</strong>. But I'm still encountering this error. <strong>Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type.</strong></p>
  
  <pre><code>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);
  </code></pre>