dotnet/wpf · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

TypefaceCollection implements ICollection<Typeface> but is a read-only view: Add (and the other mutators) throw NotSupportedException. The collection only reflects the typefaces of an underlying font family; you cannot insert items into it.

Solutions

  1. Do not mutate TypefaceCollection — build a new List<Typeface> from it if modification is needed
  2. Replace the target collection with a writable List<Typeface> or ObservableCollection<Typeface>
  3. Guard writes with collection.IsReadOnly checks in generic code

Example fix

// before
collection.Add(new Typeface("Arial")); // NotSupportedException
// after
var editable = new List<Typeface>(collection) { new Typeface("Arial") };
Defensive patterns

Strategy: type-guard

Validate before calling

if (typefaceCollection.IsReadOnly)
    copy = new List<Typeface>(typefaceCollection);

Type guard

bool IsWritableCollection<T>(ICollection<T> c) => c != null && !c.IsReadOnly;

Try / catch

try { collection.Add(item); }
catch (NotSupportedException)
{
    throw new InvalidOperationException("TypefaceCollection is read-only; copy to a List<Typeface> to edit");
}

Prevention

When it happens

Trigger: Calling Add/Remove/Clear on a TypefaceCollection obtained from a FontFamily (e.g. FontFamily.FamilyNames or FamilyTypefaces), or generic code that treats any ICollection<Typeface> as writable.

Common situations: LINQ/binding code or collection-initializer syntax accidentally trying to mutate the family's typeface list, porting code that worked with List<Typeface> to TypefaceCollection.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/TypefaceCollection.cs:37

        public TypefaceCollection(FontFamily fontFamily, Text.TextInterface.FontFamily family)
        {
            _fontFamily = fontFamily;
            _family = family;
            _familyTypefaceCollection = null;
        }

        public TypefaceCollection(FontFamily fontFamily, FamilyTypefaceCollection familyTypefaceCollection)
        {
            _fontFamily = fontFamily;
            _familyTypefaceCollection = familyTypefaceCollection;
            _family = null;
        }

        #region ICollection<Typeface> Members

        public void Add(Typeface item)
        {
            throw new NotSupportedException();
        }

        public void Clear()
        {
            throw new NotSupportedException();
        }

        public bool Contains(Typeface item)
        {
            foreach (Typeface t in this)
            {
                if (t.Equals(item))
                    return true;
            }
            return false;
        }

        public void CopyTo(Typeface[] array, int arrayIndex)

View on GitHub (pinned to 81131a70a4)