dotnet/wpf · error · NotSupportedException

throw new NotSupportedException() in IList

Error message

throw new NotSupportedException() in IList<T>.IndexOf (WeakReferenceList)

What it means

WeakReferenceList explicitly implements IList<T>.IndexOf and always throws NotSupportedException: locating an item would require dereferencing and comparing every weak reference, which the list does not support. Only Add/Insert/Remove-style operations are meaningful for this internal weak-reference collection.

Solutions

  1. Do not call IndexOf on WeakReferenceList; iterate the list comparing item references manually
  2. Use Contains-by-identity via a loop over the list's indexer
  3. Copy live items into a regular List<T> first, then use standard operations

Example fix

// before
int idx = ((IList<DependencyObject>)weakList).IndexOf(item);
// after
int idx = -1;
for (int i = 0; i < weakList.Count; i++)
    if (ReferenceEquals(weakList[i], item)) { idx = i; break; }
Defensive patterns

Strategy: try-catch

Validate before calling

if (collection is WeakReferenceList) throw new InvalidOperationException("IndexOf is not supported on WeakReferenceList; iterate manually");

Type guard

bool SupportsIndexOf<T>(IList<T> l) => l is not WeakReferenceList && !(l is IList<T> il && il.GetType().GetInterfaceMap(typeof(IList<T>)) is null);

Try / catch

try { idx = list.IndexOf(item); } catch (NotSupportedException) { idx = ManualIndexOf(list, item); }

Prevention

When it happens

Trigger: Casting a WeakReferenceList to IList<T> or ICollection and calling IndexOf, Contains, or CopyTo paths that rely on IndexOf.

Common situations: LINQ queries (IndexOf-based) over schema context internal collections; generic collection code assuming full IList<T> semantics on XamlSchemaContext's internal lists.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/35b103a956082e73. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs:1419

                AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;
            }

            public void Unhook()
            {
                AppDomain.CurrentDomain.AssemblyLoad -= OnAssemblyLoad;
            }
        }

        private class WeakReferenceList<T> : List<WeakReference>, IList<T> where T : class
        {
            public WeakReferenceList(int capacity)
                : base(capacity)
            {
            }

            int IList<T>.IndexOf(T item)
            {
                throw new NotSupportedException();
            }

            void IList<T>.Insert(int index, T item)
            {
                Insert(index, new WeakReference(item));
            }

            T IList<T>.this[int index]
            {
                get
                {
                    return (T)this[index].Target;
                }
                set
                {
                    this[index] = new WeakReference(value);
                }
            }

View on GitHub (pinned to 81131a70a4)