dotnet/wpf · error · NotSupportedException

SR.General_ObjectIsReadOnly

Error message

SR.General_ObjectIsReadOnly

What it means

FamilyTypefaceCollection becomes read-only when _innerList is non-null — it then wraps a fixed list of FamilyTypeface objects (e.g. a family's existing typefaces). VerifyChangeable throws NotSupportedException for any mutating call: Remove, Insert, SetItem, Clear, RemoveAt.

Solutions

  1. Do not mutate the wrapped collection; build a new FamilyTypefaceCollection in changeable mode and add typefaces there.
  2. Create a new FontFamily with a composite font definition containing your typefaces.
  3. If you construct the collection yourself, use the changeable constructor (no inner list).

Example fix

// before
fontFamily.FamilyTypefaces.Clear(); // NotSupportedException
// after
var newCollection = new FamilyTypefaceCollection(); // changeable, no inner list
newCollection.Add(new FamilyTypeface { Weight = FontWeights.Bold });
Defensive patterns

Strategy: validation

Validate before calling

// Collection backed by an inner list is read-only; only mutate collections you constructed without an inner list.

Type guard

static bool IsChangeable(FamilyTypefaceCollection c) => c != null && IsChangeableInstance(c); // prefer constructing your own collection for mutation

Try / catch

try { collection.Add(tf); }
catch (NotSupportedException ex) { /* read-only collection; build a new one */ }

Prevention

When it happens

Trigger: Calling Add, Insert, Remove, RemoveAt, Clear, or the set indexer on a FamilyTypefaceCollection constructed with an inner list (read-only mode), e.g. FontFamily.FamilyTypefaces of a family backed by an existing typeface list.

Common situations: Trying to modify the typeface collection of an installed/system font family at runtime instead of defining your own composite font.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

                for (int i = 0; i < _count; ++i)
                {
                    if (GetItem(i).Equals(item))
                        return i;
                }
            }
            return -1;
        }

        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)

View on GitHub (pinned to 81131a70a4)