System.ComponentModel.TypeConverter.ConvertTo C# (CSharp) Method

ConvertTo() private method

private ConvertTo ( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType ) : object
context ITypeDescriptorContext
culture System.Globalization.CultureInfo
value object
destinationType Type
return object
        public virtual object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
            if (destinationType == null) {
                throw new ArgumentNullException("destinationType");
            }

            if (destinationType == typeof(string)) {
                if (value == null) {
                    return String.Empty;
                }

                if(culture != null && culture != CultureInfo.CurrentCulture) {
                    IFormattable formattable = value as IFormattable;
                    if(formattable != null) {
                        return formattable.ToString(/* format = */ null, /* formatProvider = */ culture);
                    }
                }
                return value.ToString();
            }
            throw GetConvertToException(value, destinationType);
        }

Same methods

TypeConverter::ConvertTo ( object value, Type destinationType ) : object

Usage Example

Example #1
0
        private static object DoConversion(object value, Type toType, CultureInfo culture)
        {
            if ((value is IConvertible) || (value == null))
            {
                try
                {
                    return(System.Convert.ChangeType(value, toType, culture));
                }
                catch (Exception)
                {
                    return(DependencyProperty.UnsetValue);
                }
            }
            else
            {
                System.ComponentModel.TypeConverter typeConverter = TypeDescriptor.GetConverter(value);

                if (typeConverter.CanConvertTo(toType))
                {
                    return(typeConverter.ConvertTo(null, culture, value, toType));
                }
            }

            return(DependencyProperty.UnsetValue);
        }
All Usage Examples Of System.ComponentModel.TypeConverter::ConvertTo