dotnet/wpf · error · ArgumentException

SR.General_BadType

Error message

SR.General_BadType

What it means

FontStyleConverter.ConvertFrom throws ArgumentException with General_BadType when the incoming 'value' is not a string. The converter parses only string input (via FontStyles.FontStyleStringToKnownStyle); any other object type is rejected with this ArgumentException.

Solutions

  1. Convert the value to string before calling ConvertFrom (value.ToString()).
  2. Check CanConvertFrom(value is string) before invoking.
  3. If the value is already a FontStyle, use it directly without conversion.

Example fix

// before
var style = (FontStyle)converter.ConvertFrom(someObject);
// after
var style = (FontStyle)converter.ConvertFrom(someObject?.ToString() ?? "Normal");
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 { style = (FontStyle)converter.ConvertFrom(value); } catch (ArgumentException) { style = FontStyles.Normal; }

Prevention

When it happens

Trigger: Calling FontStyleConverter.ConvertFrom with a non-string object such as a number, FontStyle instance, or null.

Common situations: Binding pipelines or XAML markup extension scenarios where the value was expected to be a string but arrives as another type (e.g. already-typed FontStyle, boxed int).

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/b37c65abc3446d36. Report an issue: GitHub.

Appendix: source

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

        /// <summary>
        /// ConvertFrom - attempt to convert to a FontStyle 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 FontStyle.
        /// </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));
            }
            
            FontStyle fontStyle = new FontStyle();
            if (!FontStyles.FontStyleStringToKnownStyle(s, ci, ref fontStyle))
                throw new FormatException(SR.Parsers_IllegalToken);

            return fontStyle;
        }

        /// <summary>
        /// TypeConverter method implementation.
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// An NotSupportedException is thrown if the example object is null or is not a FontStyle,
        /// 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)