dotnet/wpf · error · ArgumentException

SR.General_BadType

Error message

SR.General_BadType

What it means

TypeConverter.ConvertFrom for FontStretch throws ArgumentException with General_BadType when the incoming 'value' is not a string (value as string yields null). The converter only accepts string input and converts it via FontStretches.FontStretchStringToKnownStretch; any non-string object is rejected before parsing.

Solutions

  1. Pass a string to ConvertFrom (e.g. value.ToString() if the object has a meaningful textual form).
  2. If the value is already a FontStretch, skip the converter or call CanConvertFrom first.
  3. Use TypeDescriptor/Convert.ChangeType so the value is normalized to string before conversion.

Example fix

// before
var stretch = (FontStretch)converter.ConvertFrom(someFontStretchObject);
// after
var stretch = (FontStretch)converter.ConvertFrom(someFontStretchObject.ToString());
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value is string s)) throw new ArgumentException($"Expected a string, got {value?.GetType().Name}");

Type guard

bool IsConvertibleString(object v) => v is string;

Try / catch

try { stretch = (FontStretch)converter.ConvertFrom(value); } catch (ArgumentException) { stretch = FontStretches.Normal; }

Prevention

When it happens

Trigger: Calling FontStretchConverter.ConvertFrom with a non-string object, e.g. a number, FontStretch instance, or null boxed object.

Common situations: Data binding or XAML designer scenarios where a property value arrives as a non-string (e.g. an int or already-converted FontStretch) and is routed through the converter.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/233c10c6aec1dfe7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FontStretchConverter.cs:65

        /// <summary>
        /// ConvertFrom - attempt to convert to a FontStretch from the given object
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// A NotSupportedException is thrown if the example object is null or is not a valid type
        /// which can be converted to a FontStretch.
        /// </exception>
        public override object ConvertFrom(ITypeDescriptorContext td, CultureInfo ci, object value)
        {
            if (null == value)
            {
                throw GetConvertFromException(value);
            }

            String s = value as string;

            if (null == s)
            {
                throw new ArgumentException(SR.Format(SR.General_BadType, "ConvertFrom"), nameof(value));
            }
            
            FontStretch fontStretch = new FontStretch();
            if (!FontStretches.FontStretchStringToKnownStretch(s, ci, ref fontStretch))
                throw new FormatException(SR.Parsers_IllegalToken);

            return fontStretch;
        }

        /// <summary>
        /// TypeConverter method implementation.
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// An NotSupportedException is thrown if the example object is null or is not a FontStretch,
        /// or if the destinationType isn't one of the valid destination types.
        /// </exception>
        /// <param name="context">ITypeDescriptorContext</param>
        /// <param name="culture">current culture (see CLR specs)</param>

View on GitHub (pinned to 81131a70a4)