Пример #1
0
        private static object FormatObjectInternal(object value,
                                                   Type targetType,
                                                   TypeConverter sourceConverter,
                                                   TypeConverter targetConverter,
                                                   string formatString,
                                                   IFormatProvider formatInfo,
                                                   object formattedNullValue)
        {
            if (value == System.DBNull.Value || value is null)
            {
                //
                // Convert DBNull to the formatted representation of 'null' (if possible)
                //
                if (formattedNullValue is not null)
                {
                    return(formattedNullValue);
                }

                //
                // Convert DBNull or null to a specific 'known' representation of null (otherwise fail)
                //
                if (targetType == stringType)
                {
                    return(string.Empty);
                }

                if (targetType == checkStateType)
                {
                    return(CheckState.Indeterminate);
                }

                // Just pass null through: if this is a value type, it's been unwrapped here, so we return null
                // and the caller has to wrap if appropriate.
                return(null);
            }

            //
            // Special case conversions
            //

            if (targetType == stringType)
            {
                if (value is IFormattable && !string.IsNullOrEmpty(formatString))
                {
                    return((value as IFormattable).ToString(formatString, formatInfo));
                }
            }

            //The converters for properties should take precedence.  Unfortunately, we don't know whether we have one.  Check vs. the
            //type's TypeConverter.  We're punting the case where the property-provided converter is the same as the type's converter.
            Type          sourceType = value.GetType();
            TypeConverter sourceTypeTypeConverter = TypeDescriptor.GetConverter(sourceType);

            if (sourceConverter is not null && sourceConverter != sourceTypeTypeConverter && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType));
            }

            TypeConverter targetTypeTypeConverter = TypeDescriptor.GetConverter(targetType);

            if (targetConverter is not null && targetConverter != targetTypeTypeConverter && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value));
            }

            if (targetType == checkStateType)
            {
                if (sourceType == booleanType)
                {
                    return(((bool)value) ? CheckState.Checked : CheckState.Unchecked);
                }
                else
                {
                    if (sourceConverter is null)
                    {
                        sourceConverter = sourceTypeTypeConverter;
                    }

                    if (sourceConverter is not null && sourceConverter.CanConvertTo(booleanType))
                    {
                        return((bool)sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, booleanType)
                            ? CheckState.Checked : CheckState.Unchecked);
                    }
                }
            }

            if (targetType.IsAssignableFrom(sourceType))
            {
                return(value);
            }

            //
            // If explicit type converters not provided, supply default ones instead
            //

            if (sourceConverter is null)
            {
                sourceConverter = sourceTypeTypeConverter;
            }

            if (targetConverter is null)
            {
                targetConverter = targetTypeTypeConverter;
            }

            //
            // Standardized conversions
            //

            if (sourceConverter is not null && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType));
            }
Пример #2
0
        /// <devdoc>
        ///
        /// Converts a value entered by the end user (through UI) into the corresponding binary value.
        ///
        /// - Converts formatted representations of 'null' into DBNull
        /// - Performs some special-case conversions (eg. CheckState to Boolean)
        /// - Uses TypeConverters or IConvertible where appropriate
        /// - Throws a FormatException is no suitable conversion can be found
        ///
        /// </devdoc>
        private static object ParseObjectInternal(object value,
                                                  Type targetType,
                                                  Type sourceType,
                                                  TypeConverter targetConverter,
                                                  TypeConverter sourceConverter,
                                                  IFormatProvider formatInfo,
                                                  object formattedNullValue)
        {
            //
            // Convert the formatted representation of 'null' to DBNull (if possible)
            //

            if (EqualsFormattedNullValue(value, formattedNullValue, formatInfo) || value == System.DBNull.Value)
            {
                return(System.DBNull.Value);
            }

            //
            // Special case conversions
            //

            TypeConverter targetTypeTypeConverter = TypeDescriptor.GetConverter(targetType);

            if (targetConverter != null && targetTypeTypeConverter != targetConverter && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value));
            }

            TypeConverter sourceTypeTypeConverter = TypeDescriptor.GetConverter(sourceType);

            if (sourceConverter != null && sourceTypeTypeConverter != sourceConverter && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType));
            }

            if (value is string)
            {
                // If target type has a suitable Parse method, use that to parse strings
                object parseResult = InvokeStringParseMethod(value, targetType, formatInfo);
                if (parseResult != parseMethodNotFound)
                {
                    return(parseResult);
                }
            }
            else if (value is CheckState)
            {
                CheckState state = (CheckState)value;
                if (state == CheckState.Indeterminate)
                {
                    return(DBNull.Value);
                }
                // Explicit conversion from CheckState to Boolean
                if (targetType == booleanType)
                {
                    return(state == CheckState.Checked);
                }
                if (targetConverter == null)
                {
                    targetConverter = targetTypeTypeConverter;
                }
                if (targetConverter != null && targetConverter.CanConvertFrom(booleanType))
                {
                    return(targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), state == CheckState.Checked));
                }
            }
            else if (value != null && targetType.IsAssignableFrom(value.GetType()))
            {
                // If value is already of a compatible type, just go ahead and use it
                return(value);
            }

            //
            // If explicit type converters not provided, supply default ones instead
            //

            if (targetConverter == null)
            {
                targetConverter = targetTypeTypeConverter;
            }

            if (sourceConverter == null)
            {
                sourceConverter = sourceTypeTypeConverter;
            }

            //
            // Standardized conversions
            //

            if (targetConverter != null && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value));
            }
            else if (sourceConverter != null && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType));
            }
            else if (value is IConvertible)
            {
                return(ChangeType(value, targetType, formatInfo));
            }

            //
            // Fail if no suitable conversion found
            //

            throw new FormatException(GetCantConvertMessage(value, targetType));
        }