dotnet/wpf · error · ArgumentException

SR.Format(SR.General_Expected_Type, "FontFamily")

Error message

SR.Format(SR.General_Expected_Type, "FontFamily")

What it means

FontFamilyConverter.ConvertTo throws this ArgumentException when the value being converted is not a FontFamily instance. The converter only knows how to serialize FontFamily objects (to a string), and rejects any other type by parameter name 'value'.

Solutions

  1. Only call ConvertTo with an actual FontFamily instance (check 'value is FontFamily' first).
  2. Use the correct converter for the value's type (e.g. TypefaceConverter for Typeface).
  3. If converting from string, use ConvertFrom instead.

Example fix

// before
converter.ConvertTo(null, culture, "Arial", typeof(string));
// after
if (value is FontFamily ff)
    converter.ConvertTo(null, culture, ff, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not FontFamily)
    throw new ArgumentException("value must be a FontFamily", nameof(value));

Type guard

bool IsFontFamily(object v) => v is FontFamily;

Try / catch

try { var s = (string)converter.ConvertTo(null, culture, value, typeof(string)); }
catch (ArgumentException) { /* value wasn't a FontFamily */ }

Prevention

When it happens

Trigger: Calling TypeConverter.ConvertTo(null-context, culture, someNonFontFamilyObject, typeof(string)) — e.g. passing a string, Typeface, or null-derived custom object — expecting it to behave like a FontFamily.

Common situations: Generic serialization code piping arbitrary property values through the converter, or binding/converting code that forgot to validate the runtime type.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FontFamilyConverter.cs:120

                    }
                }

                return new FontFamily(baseUri, s);
            }
            return base.ConvertFrom(context, cultureInfo, o);
        }

        /// <summary>
        /// ConvertTo - Converts the specified object to an instance of the specified type.
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            ArgumentNullException.ThrowIfNull(value);

            FontFamily fontFamily = value as FontFamily;
            if (fontFamily == null)
            {
                throw new ArgumentException(SR.Format(SR.General_Expected_Type, "FontFamily"), nameof(value));
            }

            ArgumentNullException.ThrowIfNull(destinationType);

            if (destinationType == typeof(string))
            {
                if (fontFamily.Source != null)
                {
                    // Usual case: it's a named font family.
                    return fontFamily.Source;
                }
                else
                {
                    // If client calls typeConverter.CanConvertTo(typeof(string)) then we'll return
                    // true always, even though we don't have access to the FontFamily instance; so
                    // we need to be able to return some kind of family name even if Source==null.
                    string name = null;

View on GitHub (pinned to 81131a70a4)