dotnet/wpf · error · NotSupportedException

SR.General_ObjectIsReadOnly

Error message

SR.General_ObjectIsReadOnly

What it means

FamilyMapCollection wraps an underlying font-family-map collection; when the collection was created without a backing _fontInfo it is a read-only view. Any mutation (Add, Insert, Remove, Clear, RemoveAt, indexer set) calls VerifyChangeable and throws NotSupportedException to protect the read-only object.

Solutions

  1. Do not mutate the returned collection; construct a new FontFamily or a new FontFamilyMapCollection with a changeable backing (_fontInfo != null).
  2. Build a separate List<FontFamilyMap> of the desired maps and assign them via the font family's configuration instead of editing the read-only collection.
  3. If ownership is yours, ensure the collection is created via the constructor path that supplies a non-null font info.

Example fix

// before
fontFamily.FamilyMaps.Add(new FontFamilyMap { UnicodeRange = "0-FF" }); // NotSupportedException
// after
var mapList = new List<FontFamilyMap>(fontFamily.FamilyMaps) { new FontFamilyMap { UnicodeRange = "0-FF" } };
// rebuild/assign the family with mapList instead of mutating the read-only collection
Defensive patterns

Strategy: validation

Validate before calling

bool canChange = !object.ReferenceEquals(fontFamily.FamilyMaps, null) && fontFamily.FamilyMaps.GetType().GetMethod("Add") != null; // or track whether you constructed the collection changeable
if (!canChange) throw new InvalidOperationException("FamilyMapCollection is read-only; build a new one.");

Type guard

static bool IsChangeable(FamilyMapCollection c) => !IsReadOnly(c); // read-only collections throw on mutation; prefer constructing your own instance

Try / catch

try { collection.Add(map); }
catch (NotSupportedException ex) { /* collection is read-only; rebuild */ }

Prevention

When it happens

Trigger: Calling Add, Insert, Remove, RemoveAt, Clear, or the set indexer on a FamilyMapCollection whose _fontInfo is null — e.g. modifying FontFamily.FamilyMaps obtained from a read-only/frozen font family instance.

Common situations: Trying to edit FontFamily.FamilyMaps at runtime for a font family that exposes a read-only collection, or after the collection was built for a sealed/read-only font family.

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

Appendix: source

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

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

        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;

View on GitHub (pinned to 81131a70a4)