dotnet/wpf · error · ArgumentException

SR.Format(SR.CannotConvertType, obj.GetType()…

Error message

SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FontFamilyMap))

What it means

ConvertValue validates that the object supplied to FamilyMapCollection's IList-style Add/Insert is actually a FontFamilyMap. Because the collection implements non-generic IList, the runtime type cannot be checked at compile time, so an invalid object becomes ArgumentException with CannotConvertType naming the actual type and the expected FontFamilyMap.

Solutions

  1. Only add FontFamilyMap instances: new FontFamilyMap { UnicodeRange = "...", Family = "..." }.
  2. If the item comes as object, cast or pattern-match before inserting.
  3. If a conversion is intended (e.g. from a string range), convert to FontFamilyMap explicitly before adding.

Example fix

// before
((IList)collection).Add("U+0041-005A; Arial"); // ArgumentException CannotConvertType
// after
var map = new FontFamilyMap { UnicodeRange = "U+0041-005A", Family = "Arial" };
((IList)collection).Add(map);
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is not FontFamilyMap) throw new ArgumentException($"Expected FontFamilyMap, got {obj?.GetType().Name}");

Type guard

static bool IsFamilyMap(object o) => o is FontFamilyMap;

Try / catch

try { ((IList)collection).Add(obj); }
catch (ArgumentException ex) when (ex.Message.Contains("CannotConvertType")) { /* wrong item type */ }

Prevention

When it happens

Trigger: Calling Add(object) or Insert(index, object) through the non-generic IList interface with something other than a FontFamilyMap, or constructing/inserting wrong-typed items via the collection's internal constructor path.

Common situations: Code that treats the collection as IList and adds items read from config, XAML databinding leftovers, or a heterogeneous list of media objects.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs:367

        private void RangeCheck(int index)
        {
            ArgumentOutOfRangeException.ThrowIfNegative(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _count);
        }

        private void VerifyChangeable()
        {
            if (_fontInfo == null)
                throw new NotSupportedException(SR.General_ObjectIsReadOnly);
        }

        private FontFamilyMap ConvertValue(object obj)
        {
            ArgumentNullException.ThrowIfNull(obj);

            FontFamilyMap familyMap = obj as FontFamilyMap;
            if (familyMap == null)
                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FontFamilyMap)));

            return familyMap;
        }

        private class Enumerator : IEnumerator<FontFamilyMap>, SC.IEnumerator
        {
            private FontFamilyMap[] _items;
            private int _count;
            private int _index;
            private FontFamilyMap _current;

            internal Enumerator(FontFamilyMap[] items, int count)
            {
                _items = items;
                _count = count;
                _index = -1;
                _current = null;
            }

View on GitHub (pinned to 81131a70a4)