Example #1
0
        private static object ParseObjectInternal(
            object value,
            System.Type targetType,
            System.Type sourceType,
            TypeConverter targetConverter,
            TypeConverter sourceConverter,
            IFormatProvider formatInfo,
            object formattedNullValue)
        {
            if (Formatter.EqualsFormattedNullValue(value, formattedNullValue, formatInfo) || value == DBNull.Value)
            {
                return((object)DBNull.Value);
            }
            TypeConverter converter1 = TypeDescriptor.GetConverter(targetType);

            if (targetConverter != null && converter1 != targetConverter && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value));
            }
            TypeConverter converter2 = TypeDescriptor.GetConverter(sourceType);

            if (sourceConverter != null && converter2 != sourceConverter && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value, targetType));
            }
            if (value is string)
            {
                object method = Formatter.InvokeStringParseMethod(value, targetType, formatInfo);
                if (method != Formatter.parseMethodNotFound)
                {
                    return(method);
                }
            }
            else if (value is CheckState)
            {
                CheckState checkState = (CheckState)value;
                if (checkState == CheckState.Indeterminate)
                {
                    return((object)DBNull.Value);
                }
                if ((object)targetType == (object)Formatter.booleanType)
                {
                    return((object)(checkState == CheckState.Checked));
                }
                if (targetConverter == null)
                {
                    targetConverter = converter1;
                }
                if (targetConverter != null && targetConverter.CanConvertFrom(Formatter.booleanType))
                {
                    return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), (object)(checkState == CheckState.Checked)));
                }
            }
            else if (value != null && targetType.IsAssignableFrom(value.GetType()))
            {
                return(value);
            }
            if (targetConverter == null)
            {
                targetConverter = converter1;
            }
            if (sourceConverter == null)
            {
                sourceConverter = converter2;
            }
            if (targetConverter != null && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value));
            }
            if (sourceConverter != null && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value, targetType));
            }
            if (!(value is IConvertible))
            {
                throw new FormatException(Formatter.GetCantConvertMessage(value, targetType));
            }
            return(Formatter.ChangeType(value, targetType, formatInfo));
        }