/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <returns> /// An <see cref="T:System.Object"/> that represents the converted value. /// </returns> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context. </param> /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture. </param> /// <param name="value">The <see cref="T:System.Object"/> to convert. </param> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string @string = value as string; if (@string != null) { Duration duration; if (TimeHelpers.GetDurationPatterns(culture).TryParseAny(@string.Trim(), out duration)) { return(duration); } TimeSpan timeSpan; if (TimeSpan.TryParse(@string, out timeSpan)) { return(Duration.FromTimeSpan(timeSpan)); } throw new NotSupportedException( // ReSharper disable once AssignNullToNotNullAttribute string.Format(Resources.DurationConverter_ConvertFrom_CannotParse, @string)); } if (value is TimeSpan) { TimeSpan timeSpan = (TimeSpan)value; return(Duration.FromTimeSpan(timeSpan)); } return(base.ConvertFrom(context, culture, value)); }