dotnet/wpf · error · ArgumentException

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

Error message

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

What it means

ConvertValue enforces that items passed to FamilyTypefaceCollection's IList-style Add/Insert are FamilyTypeface instances. Any other object raises ArgumentException with CannotConvertType, reporting the supplied object's actual type and the expected FamilyTypeface type.

Solutions

  1. Only insert FamilyTypeface instances.
  2. Pattern-match/cast the object and construct a FamilyTypeface with the intended values before inserting.
  3. Fix upstream producers (binding, reflection) so they emit FamilyTypeface objects.

Example fix

// before
((IList)collection).Add(FontWeights.Bold); // ArgumentException CannotConvertType
// after
var tf = new FamilyTypeface { Weight = FontWeights.Bold };
((IList)collection).Add(tf);
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

static bool IsFamilyTypeface(object o) => o is FamilyTypeface;

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) via the non-generic IList interface with a non-FamilyTypeface object, or adding items via the collection's constructor path with wrong types.

Common situations: Reflection or databinding code that inserts loosely-typed objects (strings, generic glyph descriptions) into the typeface collection.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs:359

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

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

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

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

            return familyTypeface;
        }

        private void CopyItems(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);

            if (array.Rank != 1)
                throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);

            Type elementType = array.GetType().GetElementType();
            if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))
                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[]), elementType));

            if (index >= array.Length)
                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));

View on GitHub (pinned to 81131a70a4)