/// <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> /// Computes a hash value of the specified <paramref name="value"/>. /// </summary> /// <param name="value">The object to compute a hash code for.</param> /// <param name="setup">The <see cref="HashOptions"/> which need to be configured.</param> /// <returns>A <see cref="HashResult"/> containing the computed hash value of the specified <paramref name="value"/>.</returns> public static HashResult ComputeHash(object value, Action <HashOptions> setup = null) { var options = setup.ConfigureOptions(); return(ComputeHash(EnumerableConverter.AsArray(value), o => o.AlgorithmType = options.AlgorithmType)); }