/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <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> /// <returns> /// An <see cref="T:System.Object"/> that represents the converted value. /// </returns> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (!(value is string)) { return(base.ConvertFrom(context, culture, value)); } string[] values = (value as string).Split(new[] { ',' }); if (values.Length != 3) { return(base.ConvertFrom(context, culture, value)); } LevelsSettings result = new LevelsSettings(); try { result.Minimum = int.Parse(values[0]); result.Median = float.Parse(values[1]); result.Maximum = int.Parse(values[2]); } catch { return(base.ConvertFrom(context, culture, value)); } return(result); }
/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param> /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo"/>. If null is passed, the current culture is assumed.</param> /// <param name="value">The <see cref="T:System.Object"/> to convert.</param> /// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to.</param> /// <returns> /// An <see cref="T:System.Object"/> that represents the converted value. /// </returns> /// <exception cref="T:System.ArgumentNullException">The <paramref name="destinationType"/> parameter is null. </exception> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) { if (destinationType != typeof(string) || !(value is LevelsSettings)) { return(base.ConvertTo(context, culture, value, destinationType)); } LevelsSettings settings = value as LevelsSettings; return(string.Format("{0}, {1}, {2}", settings.Minimum, settings.Median, settings.Maximum)); }