/// <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, CultureInfo culture, object value, Type destinationType) { // Get the original text var ret = _baseConverter.ConvertTo(context, culture, value, destinationType); string extra = null; // Check if we will be showing the extra text if (value != null && destinationType == typeof(string) && context.ShowExtendedText()) { // Get the extra text provider func Func <object, string> extraTextProvider; lock (_extraTextProvidersSync) { _extraTextProviders.TryGetValue(value.GetType(), out extraTextProvider); } // Get the extra text if a provider was found if (extraTextProvider != null) { try { extra = extraTextProvider(value); } catch (Exception ex) { // When there is an exception when trying to get the extra text, do not let it annoy the // end-user since its just, well, extra text. We will be fine without it. However, show it // in the logs and while Debug is defined so we can still debug it. const string errmsg = "Failed to acquire the extra text for type `{0}` on instance `{1}`. Reason: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, value.GetType(), value, ex); } Debug.Fail(string.Format(errmsg, value.GetType(), value, ex)); extra = null; } } } // Append the extra text if we have it if (!string.IsNullOrEmpty(extra)) { // Make sure the ret string is not null before appending to it if (ret == null) { ret = string.Empty; } ret += " [" + extra + "]"; } return(ret); }