/// <summary> /// Converts the given object, when it is of type <see cref="string"/> to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param> /// <param name="value">The System.Object to convert.</param> /// <returns>An <see cref="IQuantity"/> object.</returns> /// <exception cref="System.NotSupportedException">The conversion cannot be performed.</exception> /// <exception cref="ArgumentException">Unit value is not a know unit enum type.</exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string stringValue = value as string; object result = null; if (!string.IsNullOrEmpty(stringValue)) { if (double.TryParse(stringValue, NumberStyles.Any, culture, out double dvalue)) { DefaultUnitAttribute defaultUnit = GetAttribute <DefaultUnitAttribute>(context) ?? new DefaultUnitAttribute(default(TQuantity).Unit); result = Quantity.From(dvalue, defaultUnit.UnitType); } else { result = Quantity.Parse(culture, typeof(TQuantity), stringValue); } ConvertToUnitAttribute convertToUnit = GetAttribute <ConvertToUnitAttribute>(context); if (convertToUnit != null) { result = ((IQuantity)result).ToUnit(convertToUnit.UnitType); } } return(result ?? base.ConvertFrom(context, culture, value)); }
/// <summary> /// Converts the given object, when it is of type <see cref="string"/> to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param> /// <param name="value">The System.Object to convert.</param> /// <returns>An <see cref="IQuantity"/> object.</returns> /// <exception cref="System.NotSupportedException">The conversion cannot be performed.</exception> /// <exception cref="ArgumentException">Unit value is not a know unit enum type.</exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string stringValue = value as string; object result = null; if (!string.IsNullOrEmpty(stringValue)) { if (double.TryParse(stringValue, NumberStyles.Any, culture, out double dvalue)) { DefaultUnitAttribute defaultUnit = GetAttribute <DefaultUnitAttribute>(context) ?? new DefaultUnitAttribute(default(TQuantity).Unit); result = Quantity.From(dvalue, defaultUnit.UnitType); } else { // TODO this should not be part of QuantityTypeConverter. it should rather be part of the parse function stringValue = stringValue.Replace("^-9", "⁻⁹"); stringValue = stringValue.Replace("^-8", "⁻⁸"); stringValue = stringValue.Replace("^-7", "⁻⁷"); stringValue = stringValue.Replace("^-6", "⁻⁶"); stringValue = stringValue.Replace("^-5", "⁻⁵"); stringValue = stringValue.Replace("^-4", "⁻⁴"); stringValue = stringValue.Replace("^-3", "⁻³"); stringValue = stringValue.Replace("^-2", "⁻²"); stringValue = stringValue.Replace("^-1", "⁻¹"); stringValue = stringValue.Replace("^1", ""); stringValue = stringValue.Replace("^2", "²"); stringValue = stringValue.Replace("^3", "³"); stringValue = stringValue.Replace("^4", "⁴"); stringValue = stringValue.Replace("^5", "⁵"); stringValue = stringValue.Replace("^6", "⁶"); stringValue = stringValue.Replace("^7", "⁷"); stringValue = stringValue.Replace("^8", "⁸"); stringValue = stringValue.Replace("^9", "⁹"); stringValue = stringValue.Replace("*", "·"); result = Quantity.Parse(culture, typeof(TQuantity), stringValue); } ConvertToUnitAttribute convertToUnit = GetAttribute <ConvertToUnitAttribute>(context); if (convertToUnit != null) { result = ((IQuantity)result).ToUnit(convertToUnit.UnitType); } } return(result ?? base.ConvertFrom(context, culture, value)); }