{"record":{"id":"cfcbf9a7bea50bca","repo":"dotnet/maui","slug":"void-icollection-element-add-element-item-thr","errorCode":null,"errorMessage":"void ICollection<Element>.Add(Element item) => throw new NotSupportedException();","messagePattern":"void ICollection<Element>\\.Add\\(Element item\\) => throw new NotSupportedException\\(\\);","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"warning","filePath":"src/Controls/src/Core/Element/Element.cs","lineNumber":1201,"sourceCode":"\t\t\treturn false;\n\t\t}\n\n\t\tclass TemporaryWrapper : IList<Element>\n\t\t{\n\t\t\tIReadOnlyList<Element> _inner;\n\n\t\t\tpublic TemporaryWrapper(IReadOnlyList<Element> inner)\n\t\t\t{\n\t\t\t\t_inner = inner;\n\t\t\t}\n\n\t\t\tElement IList<Element>.this[int index] { get => _inner[index]; set => throw new NotSupportedException(); }\n\n\t\t\tint ICollection<Element>.Count => _inner.Count;\n\n\t\t\tbool ICollection<Element>.IsReadOnly => true;\n\n\t\t\tvoid ICollection<Element>.Add(Element item) => throw new NotSupportedException();\n\n\t\t\tvoid ICollection<Element>.Clear() => throw new NotSupportedException();\n\n\t\t\tbool ICollection<Element>.Contains(Element item) => _inner.IndexOf(item) != -1;\n\n\t\t\tvoid ICollection<Element>.CopyTo(Element[] array, int arrayIndex) => throw new NotSupportedException();\n\n\t\t\tIEnumerator<Element> IEnumerable<Element>.GetEnumerator() => _inner.GetEnumerator();\n\n\t\t\tSystem.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _inner.GetEnumerator();\n\n\t\t\tint IList<Element>.IndexOf(Element item) => _inner.IndexOf(item);\n\n\t\t\tvoid IList<Element>.Insert(int index, Element item) => throw new NotSupportedException();\n\n\t\t\tbool ICollection<Element>.Remove(Element item) => throw new NotSupportedException();\n\n\t\t\tvoid IList<Element>.RemoveAt(int index) => throw new NotSupportedException();","sourceCodeStart":1183,"sourceCodeEnd":1219,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Element/Element.cs#L1183-L1219","documentation":"Thrown by TemporaryWrapper.Add(Element). TemporaryWrapper is a read-only IList<Element> adapter (IsReadOnly = true) used internally by the obsolete Element.LogicalChildren property to expose children as a ReadOnlyCollection. All mutating operations throw NotSupportedException.","triggerScenarios":"Obtaining the internal TemporaryWrapper (e.g., through reflection or inside Hot Reload code) and calling Add on it. The public LogicalChildren returns a ReadOnlyCollection<Element> that already guards against this; reaching TemporaryWrapper.Add requires bypassing that guard.","commonSituations":"Reflection/serialization frameworks enumerating and mutating internal collections; Hot Reload or designer tooling that writes to the backing list; incorrect assumption that LogicalChildren is mutable.","solutions":["Use InsertLogicalChild to add logical children instead of mutating the collection.","Treat LogicalChildren as read-only; never write through it or its backing wrapper.","Avoid reflecting into Element internals."],"exampleFix":"// before (reflection on internal wrapper)\nwrapper.Add(child); // NotSupportedException\n// after\nelement.InsertLogicalChild(element.LogicalChildren.Count, child);","handlingStrategy":"validation","validationCode":"if (element.LogicalChildren.IsReadOnly)\n    return; // do not call Add on the backing wrapper","typeGuard":null,"tryCatchPattern":"// Adapter reached only via reflection; callers should use InsertLogicalChild instead.","preventionTips":["Add children via InsertLogicalChild, not by mutating LogicalChildren.","Treat the returned collection as read-only.","Do not reflect into Element internals."],"tags":["maui","readonly-collection","logicalchildren","notsupported"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}