{"record":{"id":"add307f0ca302516","repo":"dotnet/wpf","slug":"sr-general-objectisreadonly","errorCode":null,"errorMessage":"SR.General_ObjectIsReadOnly","messagePattern":"SR\\.General_ObjectIsReadOnly","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs","lineNumber":358,"sourceCode":"                for (int i = 0; i < _count; ++i)\n                {\n                    if (_items[i].Equals(item))\n                        return i;\n                }\n            }\n            return -1;\n        }\n\n        private void RangeCheck(int index)\n        {\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _count);\n        }\n\n        private void VerifyChangeable()\n        {\n            if (_fontInfo == null)\n                throw new NotSupportedException(SR.General_ObjectIsReadOnly);\n        }\n\n        private FontFamilyMap ConvertValue(object obj)\n        {\n            ArgumentNullException.ThrowIfNull(obj);\n\n            FontFamilyMap familyMap = obj as FontFamilyMap;\n            if (familyMap == null)\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FontFamilyMap)));\n\n            return familyMap;\n        }\n\n        private class Enumerator : IEnumerator<FontFamilyMap>, SC.IEnumerator\n        {\n            private FontFamilyMap[] _items;\n            private int _count;\n            private int _index;","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs#L340-L376","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Do not mutate the returned collection; construct a new FontFamily or a new FontFamilyMapCollection with a changeable backing (_fontInfo != null).","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.","If ownership is yours, ensure the collection is created via the constructor path that supplies a non-null font info."],"exampleFix":"// before\nfontFamily.FamilyMaps.Add(new FontFamilyMap { UnicodeRange = \"0-FF\" }); // NotSupportedException\n// after\nvar mapList = new List<FontFamilyMap>(fontFamily.FamilyMaps) { new FontFamilyMap { UnicodeRange = \"0-FF\" } };\n// rebuild/assign the family with mapList instead of mutating the read-only collection","handlingStrategy":"validation","validationCode":"bool canChange = !object.ReferenceEquals(fontFamily.FamilyMaps, null) && fontFamily.FamilyMaps.GetType().GetMethod(\"Add\") != null; // or track whether you constructed the collection changeable\nif (!canChange) throw new InvalidOperationException(\"FamilyMapCollection is read-only; build a new one.\");","typeGuard":"static bool IsChangeable(FamilyMapCollection c) => !IsReadOnly(c); // read-only collections throw on mutation; prefer constructing your own instance","tryCatchPattern":"try { collection.Add(map); }\ncatch (NotSupportedException ex) { /* collection is read-only; rebuild */ }","preventionTips":["Treat FontFamily.FamilyMaps as read-only output.","Build your own FontFamilyMapCollection when mutation is needed.","Guard mutation behind an ownership check."],"tags":["wpf","fonts","read-only-collection","not-supported"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}