public static bool TryConvertTo(this object obj, Type convertTo, out object outValue) { outValue = null; if (obj == null) { return(convertTo.IsNullableType()); } object converted; if (!ObjectConverterter.TryConvert(obj.GetType(), obj, convertTo, out converted)) { return(false); } outValue = converted; return(true); }
public static object ConvertTo(this object obj, Type convertTo) { if (obj == null || obj == DBNull.Value) { if (convertTo.IsNullableType() || convertTo.IsClass) { return(null); } throw new InvalidCastException(string.Format("Unable to cast null to {1}'.", obj.GetType(), convertTo)); } object converted = null; if (!ObjectConverterter.TryConvert(obj.GetType(), obj, convertTo, out converted)) { throw new InvalidCastException(string.Format("Unable to cast '{0}' to {1}'.", obj.GetType(), convertTo)); } return(converted); }