dotnet/maui · warning · NotSupportedException

void IList<Element>.RemoveAt(int index) => throw new NotSupp

Error message

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

What it means

Thrown by TemporaryWrapper.RemoveAt(int). TemporaryWrapper is the read-only IList<Element> adapter backing the obsolete Element.LogicalChildren ReadOnlyCollection. At the IList contract level removal-by-index is unsupported because the inner IReadOnlyList cannot be mutated and IsReadOnly is true.

Source

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

			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 RemoveLogicalChild to remove children, or remove then re-insert to reposition.
  2. Treat LogicalChildren as read-only.
  3. Do not manipulate internal Element collections via reflection.

Example fix

// before (reflection on internal wrapper)
wrapper.RemoveAt(0); // NotSupportedException
// after
element.RemoveLogicalChild(element.LogicalChildren[0]);
Defensive patterns

Strategy: validation

Validate before calling

if (element.LogicalChildren.IsReadOnly)
    return; // use RemoveLogicalChild instead

Try / catch

// Adapter reached only via reflection; remove via RemoveLogicalChild instead.

Prevention

When it happens

Trigger: Obtaining the internal TemporaryWrapper (reflection/internal access) and calling RemoveAt. The public ReadOnlyCollection blocks this; the adapter must be accessed directly for the throw to fire.

Common situations: Reflection-based tooling or Hot Reload internals attempting to remove a child by index from the backing collection; assuming the wrapper is mutable.

Related errors


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