dotnet/wpf · error · NotSupportedException
throw new NotSupportedException() in ICollection
Error message
throw new NotSupportedException() in ICollection<T>.Remove (WeakReferenceList)
What it means
WeakReferenceList implements ICollection<T> as a read-style collection of weak references; item removal via the explicit ICollection<T>.Remove interface method is intentionally not supported and always throws NotSupportedException. The collection is designed to be mutated only through its own internal mechanisms (appending, compaction of dead weak references), not by callers removing arbitrary items.
Solutions
- Do not remove items via ICollection<T>.Remove; rebuild the list without the unwanted item instead
- Use the type's own supported mutation methods (Add/internal compaction) rather than interface-based removal
- Copy elements to a List<T> (skipping the unwanted item or dead references), operate on that, and rebuild the WeakReferenceList
- If you own the call site, check ICollection<T>.IsReadOnly semantics and avoid Remove on this collection type
Example fix
// before
ICollection<WeakReference> list = weakRefList;
list.Remove(item); // throws NotSupportedException
// after
var keep = new List<WeakReference>();
for (int i = 0; i < weakRefList.Count; i++)
if (!ReferenceEquals(weakRefList[i], item)) keep.Add(weakRefList[i]);
// rebuild/reassign from `keep` using supported APIs Defensive patterns
Strategy: try-catch
Validate before calling
if (coll is ICollection<T> icoll && icoll.Contains(item)) { /* Remove unsupported: rebuild instead */ } Type guard
bool supportsRemove = collection is ICollection<T> && !(collection.GetType().Name == "WeakReferenceList");
Try / catch
try { ((ICollection<T>)weakRefList).Remove(item); }
catch (NotSupportedException) { /* rebuild list without item via supported APIs */ } Prevention
- Treat WeakReferenceList as append/inspect-only; never mutate via ICollection<T>
- Check SupportedCapabilities/IsReadOnly conventions before interface-based removal
- Copy to List<T> for element-level operations
When it happens
Trigger: Calling Remove(item) on a WeakReferenceList instance through the ICollection<T> interface, e.g. casting it to ICollection<T> and calling Remove, or passing it to code that uses the generic collection interface to remove elements. Calling the non-interface Remove overloads of the class does not hit this path.
Common situations: Developers treat the list as a normal ICollection<T> in LINQ/algorithm code and attempt in-place element removal; reflection-based or serialization code enumerates ICollection<T> members and invokes Remove; code refactored from List<T> to WeakReferenceList assumes removal is supported.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- NotSupportedException(SR.MissingCaseXamlNodes)
- SR.Format(SR.AmbiguousCollectionItemType, type)
- SR.MarkupExtensionDynamicOrBindingInCollection
- SR.NoAddMethodFound
- SR.NotSupportedOnUnknownType
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9082701c21033ee5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs:1467
{
return true;
}
}
return false;
}
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
for (int i = 0; i < Count; i++)
{
array[i + arrayIndex] = (T)this[i].Target;
}
}
bool ICollection<T>.Remove(T item)
{
throw new NotSupportedException();
}
bool ICollection<T>.IsReadOnly
{
get { return false; }
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return Enumerate().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
private IEnumerable<T> Enumerate()View on GitHub (pinned to 81131a70a4)