{"record":{"id":"35b103a956082e73","repo":"dotnet/wpf","slug":"throw-new-notsupportedexception-in-ilist-t-indexof","errorCode":null,"errorMessage":"throw new NotSupportedException() in IList<T>.IndexOf (WeakReferenceList)","messagePattern":"throw new NotSupportedException\\(\\) in IList<T>\\.IndexOf \\(WeakReferenceList\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs","lineNumber":1419,"sourceCode":"                AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;\n            }\n\n            public void Unhook()\n            {\n                AppDomain.CurrentDomain.AssemblyLoad -= OnAssemblyLoad;\n            }\n        }\n\n        private class WeakReferenceList<T> : List<WeakReference>, IList<T> where T : class\n        {\n            public WeakReferenceList(int capacity)\n                : base(capacity)\n            {\n            }\n\n            int IList<T>.IndexOf(T item)\n            {\n                throw new NotSupportedException();\n            }\n\n            void IList<T>.Insert(int index, T item)\n            {\n                Insert(index, new WeakReference(item));\n            }\n\n            T IList<T>.this[int index]\n            {\n                get\n                {\n                    return (T)this[index].Target;\n                }\n                set\n                {\n                    this[index] = new WeakReference(value);\n                }\n            }","sourceCodeStart":1401,"sourceCodeEnd":1437,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs#L1401-L1437","documentation":"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.","triggerScenarios":"Casting a WeakReferenceList to IList<T> or ICollection and calling IndexOf, Contains, or CopyTo paths that rely on IndexOf.","commonSituations":"LINQ queries (IndexOf-based) over schema context internal collections; generic collection code assuming full IList<T> semantics on XamlSchemaContext's internal lists.","solutions":["Do not call IndexOf on WeakReferenceList; iterate the list comparing item references manually","Use Contains-by-identity via a loop over the list's indexer","Copy live items into a regular List<T> first, then use standard operations"],"exampleFix":"// before\nint idx = ((IList<DependencyObject>)weakList).IndexOf(item);\n// after\nint idx = -1;\nfor (int i = 0; i < weakList.Count; i++)\n    if (ReferenceEquals(weakList[i], item)) { idx = i; break; }","handlingStrategy":"try-catch","validationCode":"if (collection is WeakReferenceList) throw new InvalidOperationException(\"IndexOf is not supported on WeakReferenceList; iterate manually\");","typeGuard":"bool SupportsIndexOf<T>(IList<T> l) => l is not WeakReferenceList && !(l is IList<T> il && il.GetType().GetInterfaceMap(typeof(IList<T>)) is null);","tryCatchPattern":"try { idx = list.IndexOf(item); } catch (NotSupportedException) { idx = ManualIndexOf(list, item); }","preventionTips":["Treat WeakReferenceList as an add/remove-only collection","Do not LINQ-index internal schema context collections","Copy to List<T> before searching"],"tags":["xaml","not-supported","collection","wpf"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}