dotnet/maui · warning · NotSupportedException

void ICollection .Clear() => throw new…

Error message

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

What it means

Thrown by TemporaryWrapper.Clear(). TemporaryWrapper is the read-only IList<Element> adapter behind the obsolete Element.LogicalChildren ReadOnlyCollection. Because IsReadOnly is true and the inner IReadOnlyList cannot be cleared, Clear throws NotSupportedException.

Solutions

  1. Remove logical children individually via RemoveLogicalChild rather than clearing the collection.
  2. Treat LogicalChildren and its backing list as read-only.
  3. Avoid writing to internal Element collections.

Example fix

// before (reflection on internal wrapper)
wrapper.Clear(); // NotSupportedException
// after
foreach (var child in element.LogicalChildren.ToList())
    element.RemoveLogicalChild(child);
Defensive patterns

Strategy: validation

Validate before calling

if (element.LogicalChildren.IsReadOnly)
    return; // do not Clear the backing wrapper

Try / catch

// Adapter reached only via reflection; remove children individually via RemoveLogicalChild.

Prevention

When it happens

Trigger: Calling Clear on the internal TemporaryWrapper obtained via reflection or internal access. The public ReadOnlyCollection wrapper would block this first; reaching TemporaryWrapper.Clear requires accessing the adapter directly.

Common situations: Hot Reload or designer tooling attempting to reset logical children by clearing the backing collection; reflection-based code treating the wrapper as mutable.

Related errors


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

Appendix: source

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

		class TemporaryWrapper : IList<Element>
		{
			IReadOnlyList<Element> _inner;

			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)