{"record":{"id":"c926fe9cb59c18bc","repo":"dotnet/maui","slug":"void-icollection-element-copyto-element-array","errorCode":null,"errorMessage":"void ICollection<Element>.CopyTo(Element[] array, int arrayIndex) => throw new NotSupportedException();","messagePattern":"void ICollection<Element>\\.CopyTo\\(Element\\[\\] array, int arrayIndex\\) => throw new NotSupportedException\\(\\);","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"warning","filePath":"src/Controls/src/Core/Element/Element.cs","lineNumber":1207,"sourceCode":"\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();\n\t\t}\n\t}\n}\n","sourceCodeStart":1189,"sourceCodeEnd":1223,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Element/Element.cs#L1189-L1223","documentation":"Thrown by TemporaryWrapper.CopyTo(Element[], int). Although CopyTo is conceptually a read operation, the TemporaryWrapper adapter deliberately throws NotSupportedException for it (only Count, Contains, IndexOf, GetEnumerator, and the indexer getter are implemented). It backs the obsolete Element.LogicalChildren ReadOnlyCollection.","triggerScenarios":"Calling CopyTo on the internal TemporaryWrapper obtained through reflection/internal access. The public ReadOnlyCollection<Element> exposes its own CopyTo that does not delegate to this throwing implementation, so this fires only when the wrapper is used directly.","commonSituations":"Internal tooling or reflection code that obtains TemporaryWrapper and calls CopyTo; incorrectly assuming the read-only adapter implements all read operations.","solutions":["Use the public LogicalChildren ReadOnlyCollection's CopyTo (which works) rather than the internal wrapper.","Iterate via GetEnumerator (implemented) instead of CopyTo.","Avoid reflecting into the internal adapter."],"exampleFix":"// before (reflection on internal wrapper)\nwrapper.CopyTo(arr, 0); // NotSupportedException\n// after\nelement.LogicalChildren.CopyTo(arr, 0); // public ReadOnlyCollection works","handlingStrategy":"validation","validationCode":"// Use the public ReadOnlyCollection.CopyTo (works), not the internal wrapper.\nelement.LogicalChildren.CopyTo(arr, index);","typeGuard":null,"tryCatchPattern":"// Adapter reached only via reflection; use the public ReadOnlyCollection API instead.","preventionTips":["Use element.LogicalChildren.CopyTo rather than the internal adapter.","Iterate via the public collection instead of reflecting into internals.","Treat the wrapper as read-only with limited read operations."],"tags":["maui","readonly-collection","logicalchildren","notsupported","copyto"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}