public static bool TryConvert <T>(object initialValue, CultureInfo culture, out T convertedValue) { return(MiscellaneousUtils.TryAction <T>(() => { object obj; ConvertUtils.TryConvert(initialValue, CultureInfo.CurrentCulture, typeof(T), out obj); return (T)obj; }, out convertedValue)); }
/// <summary> /// Converts the value to the specified type. If the value is unable to be converted, the /// value is checked whether it assignable to the specified type. /// </summary> /// <param name="initialValue">The value to convert.</param> /// <param name="culture">The culture to use when converting.</param> /// <param name="targetType">The type to convert or cast the value to.</param> /// <returns> /// The converted type. If conversion was unsuccessful, the initial value /// is returned if assignable to the target type. /// </returns> public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType) { if (targetType == typeof(object)) { return(initialValue); } if (initialValue == null && ReflectionUtils.IsNullable(targetType)) { return((object)null); } object obj; return(ConvertUtils.TryConvert(initialValue, culture, targetType, out obj) ? obj : ConvertUtils.EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType)); }
public static bool TryConvert <T>(object initialValue, out T convertedValue) { return(ConvertUtils.TryConvert <T>(initialValue, CultureInfo.get_CurrentCulture(), out convertedValue)); }