dotnet/maui · warning · NotSupportedException

void ICollection<Element>.CopyTo(Element[] array, int arrayI

Error message

void ICollection<Element>.CopyTo(Element[] array, int arrayIndex) => throw new NotSupportedException();

What it means

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.

Source

Thrown at src/Controls/src/Core/Element/Element.cs:1207

			public TemporaryWrapper(IReadOnlyList<Element> inner)
			{
				_inner = inner;
			}

			Element IList<Element>.this[int index] { get => _inner[index]; set => throw new NotSupportedException(); }

			int ICollection<Element>.Count => _inner.Count;

			bool ICollection<Element>.IsReadOnly => true;

			void ICollection<Element>.Add(Element item) => throw new NotSupportedException();

			void ICollection<Element>.Clear() => throw new NotSupportedException();

			bool ICollection<Element>.Contains(Element item) => _inner.IndexOf(item) != -1;

			void ICollection<Element>.CopyTo(Element[] array, int arrayIndex) => throw new NotSupportedException();

			IEnumerator<Element> IEnumerable<Element>.GetEnumerator() => _inner.GetEnumerator();

			System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _inner.GetEnumerator();

			int IList<Element>.IndexOf(Element item) => _inner.IndexOf(item);

			void IList<Element>.Insert(int index, Element item) => throw new NotSupportedException();

			bool ICollection<Element>.Remove(Element item) => throw new NotSupportedException();

			void IList<Element>.RemoveAt(int index) => throw new NotSupportedException();
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use the public LogicalChildren ReadOnlyCollection's CopyTo (which works) rather than the internal wrapper.
  2. Iterate via GetEnumerator (implemented) instead of CopyTo.
  3. Avoid reflecting into the internal adapter.

Example fix

// before (reflection on internal wrapper)
wrapper.CopyTo(arr, 0); // NotSupportedException
// after
element.LogicalChildren.CopyTo(arr, 0); // public ReadOnlyCollection works
Defensive patterns

Strategy: validation

Validate before calling

// Use the public ReadOnlyCollection.CopyTo (works), not the internal wrapper.
element.LogicalChildren.CopyTo(arr, index);

Try / catch

// Adapter reached only via reflection; use the public ReadOnlyCollection API instead.

Prevention

When it happens

Trigger: 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.

Common situations: Internal tooling or reflection code that obtains TemporaryWrapper and calls CopyTo; incorrectly assuming the read-only adapter implements all read operations.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/c926fe9cb59c18bc. Report an issue: GitHub.