Beispiel #1
0
        private object FormatObject(object value)
        {
            // We will not format the object when the control is in design time.
            // This is because if we bind a boolean property on a control to a
            // row that is full of DBNulls then we cause problems in the shell.
            if (ControlAtDesignTime())
            {
                return(value);
            }

            Type type = propInfo.PropertyType;

            if (formattingEnabled)
            {
                // -------------------------------
                // Behavior for Whidbey and beyond
                // -------------------------------

                // Fire the Format event so that user code gets a chance to supply the formatted value for us
                ConvertEventArgs e = new ConvertEventArgs(value, type);
                OnFormat(e);

                if (e.Value != value)
                {
                    // If event handler replaced parsed value with formatted value, use that
                    return(e.Value);
                }
                else
                {
                    // Otherwise format the parsed value ourselves
                    TypeConverter fieldInfoConverter = null;
                    if (bindToObject.FieldInfo != null)
                    {
                        fieldInfoConverter = bindToObject.FieldInfo.Converter;
                    }
                    return(Formatter.FormatObject(value, type, fieldInfoConverter, propInfoConverter, formatString, formatInfo, nullValue, dsNullValue));
                }
            }
            else
            {
                // ----------------------------
                // Behavior for RTM and Everett  [DO NOT MODIFY!]
                // ----------------------------

                // first try: use the Format event
                ConvertEventArgs e = new ConvertEventArgs(value, type);
                OnFormat(e);
                object ret = e.Value;

                // Approved breaking-change behavior between RTM and Everett: Fire the Format event even if the control property is of type
                // Object (RTM used to skip the event for properties of this type).

                if (type == typeof(object))
                {
                    return(value);
                }

                // stop now if we have a value of a compatible type
                if (ret != null && (ret.GetType().IsSubclassOf(type) || ret.GetType() == type))
                {
                    return(ret);
                }
                // second try: use type converter for the desiredType
                TypeConverter typeConverter = TypeDescriptor.GetConverter(value != null ? value.GetType() : typeof(object));
                if (typeConverter != null && typeConverter.CanConvertTo(type))
                {
                    ret = typeConverter.ConvertTo(value, type);
                    return(ret);
                }
                // last try: use Convert.ChangeType
                if (value is IConvertible)
                {
                    ret = Convert.ChangeType(value, type, CultureInfo.CurrentCulture);
                    if (ret != null && (ret.GetType().IsSubclassOf(type) || ret.GetType() == type))
                    {
                        return(ret);
                    }
                }

                // time to fail:
                throw new FormatException(SR.ListBindingFormatFailed);
            }
        }
Beispiel #2
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 != 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 != null && sourceConverter != sourceTypeTypeConverter && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType));
            }

            TypeConverter targetTypeTypeConverter = TypeDescriptor.GetConverter(targetType);

            if (targetConverter != 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 != 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 != null && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo(null, GetFormatterCulture(formatInfo), value, targetType));
            }
            else if (targetConverter != null && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom(null, GetFormatterCulture(formatInfo), value));
            }
            else if (value is IConvertible)
            {
                return(ChangeType(value, targetType, formatInfo));
            }

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

            throw new FormatException(GetCantConvertMessage(value, targetType));
        }
Beispiel #3
0
        private object ParseObject(object value)
        {
            Type type = this.bindToObject.BindToType;

            if (formattingEnabled)
            {
                // -------------------------------
                // Behavior for Whidbey and beyond
                // -------------------------------

                // Fire the Parse event so that user code gets a chance to supply the parsed value for us
                ConvertEventArgs e = new ConvertEventArgs(value, type);
                OnParse(e);

                object newValue = e.Value;

                if (!object.Equals(value, newValue))
                {
                    // If event handler replaced formatted value with parsed value, use that
                    return(newValue);
                }
                else
                {
                    // Otherwise parse the formatted value ourselves
                    TypeConverter fieldInfoConverter = null;
                    if (bindToObject.FieldInfo != null)
                    {
                        fieldInfoConverter = bindToObject.FieldInfo.Converter;
                    }
                    return(Formatter.ParseObject(value, type, (value == null ? propInfo.PropertyType : value.GetType()), fieldInfoConverter, propInfoConverter, formatInfo, nullValue, GetDataSourceNullValue(type)));
                }
            }
            else
            {
                // ----------------------------
                // Behavior for RTM and Everett  [DO NOT MODIFY!]
                // ----------------------------

                ConvertEventArgs e = new ConvertEventArgs(value, type);
                // first try: use the OnParse event
                OnParse(e);
                //
                if (e.Value != null && (e.Value.GetType().IsSubclassOf(type) || e.Value.GetType() == type || e.Value is System.DBNull))
                {
                    return(e.Value);
                }
                // second try: use the TypeConverter
                TypeConverter typeConverter = TypeDescriptor.GetConverter(value != null ? value.GetType() : typeof(object));
                if (typeConverter != null && typeConverter.CanConvertTo(type))
                {
                    return(typeConverter.ConvertTo(value, type));
                }
                // last try: use Convert.ToType
                if (value is IConvertible)
                {
                    object ret = Convert.ChangeType(value, type, CultureInfo.CurrentCulture);
                    if (ret != null && (ret.GetType().IsSubclassOf(type) || ret.GetType() == type))
                    {
                        return(ret);
                    }
                }
                // time to fail: (RTM/Everett just returns null, whereas Whidbey throws an exception)
                return(null);
            }
        }
Beispiel #4
0
        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 state)
            {
                if (state == CheckState.Indeterminate)
                {
                    return(DBNull.Value);
                }

                // Explicit conversion from CheckState to Boolean
                if (targetType == booleanType)
                {
                    return(state == CheckState.Checked);
                }

                if (targetConverter is 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 is null)
            {
                targetConverter = targetTypeTypeConverter;
            }

            if (sourceConverter is 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));
        }