/// <summary> /// Converts the specified string to its <typeparamref name="T"/> equivalent using the specified <paramref name="context"/> and <paramref name="culture"/> information. /// </summary> /// <typeparam name="T">The type of the expected return <paramref name="value"/> after conversion.</typeparam> /// <param name="value">The string value to convert.</param> /// <param name="culture">The culture-specific formatting information about <paramref name="value"/>.</param> /// <param name="context">The type-specific formatting information about <paramref name="value"/>.</param> /// <returns>An object that is equivalent to <typeparamref name="T"/> contained in <paramref name="value"/>, as specified by <paramref name="culture"/> and <paramref name="context"/>.</returns> /// <exception cref="ArgumentException"> /// Invalid <paramref name="value"/> for <typeparamref name="T"/> specified. /// </exception> /// <exception cref="NotSupportedException"> /// The conversion cannot be performed. /// </exception> public static T FromString <T>(string value, CultureInfo culture, ITypeDescriptorContext context) { try { Type resultType = typeof(T); TypeConverter converter = TypeDescriptor.GetConverter(resultType); T result = (T)converter.ConvertFromString(context, culture, value); if (resultType == typeof(Uri)) // for reasons unknown to me, MS allows all sorts of string to be constructed on a Uri - check if valid (quick-fix until more knowledge of ITypeDescriptorContext) { Uri resultAsUri = result as Uri; string[] segments = resultAsUri?.Segments; } return(result); } catch (Exception ex) { if (ex.GetType() == typeof(NotSupportedException)) { throw; } throw ExceptionUtility.Refine(ExceptionUtility.CreateArgumentException(nameof(value), ex.Message, ex.InnerException), MethodBaseConverter.FromType(typeof(Converter), EnumerableConverter.AsArray(typeof(string), typeof(CultureInfo), typeof(ITypeDescriptorContext))), value, culture, context).Unwrap(); } }
/// <summary> /// Refines the specified <paramref name="exception"/> with valuable meta information extracted from the associated <paramref name="method"/> and <paramref name="parameters"/>. /// </summary> /// <param name="exception">The exception that needs to be thrown.</param> /// <param name="method">The method signature containing valuable meta information.</param> /// <param name="parameters">The optional parameters to accompany <paramref name="method"/>.</param> /// <returns>The specified <paramref name="exception"/> refined with valuable meta information within a <see cref="MethodWrappedException"/>.</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="exception"/> is null - or - <paramref name="method"/> is null. /// </exception> public static MethodWrappedException Refine(this Exception exception, MethodDescriptor method, params object[] parameters) { return(ExceptionUtility.Refine(exception, method, parameters)); }