{"record":{"id":"7f8a1c1be17bf38a","repo":"dotnet/wpf","slug":"sr-format-sr-tablecollectionelementtypeexpected-typeof-titem","errorCode":null,"errorMessage":"SR.Format(SR.TableCollectionElementTypeExpected, typeof(TItem).Name)","messagePattern":"SR\\.Format\\(SR\\.TableCollectionElementTypeExpected, typeof\\(TItem\\)\\.Name\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs","lineNumber":455,"sourceCode":"            TItem item = value as TItem;\n\n            if (item == null)\n            {\n                return -1;\n            }\n\n            return this.IndexOf(item);\n        }\n\n        void IList.Insert(int index, object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            TItem newItem = value as TItem;\n\n            if (newItem == null)\n            {\n                throw new ArgumentException(SR.Format(SR.TableCollectionElementTypeExpected, typeof(TItem).Name), nameof(value));\n            }\n\n            this.Insert(index, newItem);\n        }\n\n        bool IList.IsFixedSize\n        {\n            get\n            {\n                return false;\n            }\n        }\n\n        bool IList.IsReadOnly\n        {\n            get\n            {\n                return this.IsReadOnly;","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs#L437-L473","documentation":"IList.Insert validates that the boxed value is assignable to TItem; otherwise it throws ArgumentException with SR.Format(SR.TableCollectionElementTypeExpected, typeof(TItem).Name). The weakly-typed IList entry point must reject elements of the wrong type because the internal storage is TItem[].","triggerScenarios":"Calling IList.Insert(index, value) (or IList.Add) with an object that is not an instance of the collection's TItem type, e.g. inserting a string into a TableRowCollection.","commonSituations":"Late-bound/reflection code or XAML/scripting interop passing loosely typed values into the non-generic IList interface; deserializers adding placeholder objects of the wrong type.","solutions":["Pass only instances of the declared element type TItem","Check 'value is TItem' before calling the IList member","Use the strongly typed generic API instead of the IList non-generic members"],"exampleFix":"// before\n((IList)collection).Insert(0, someObject);\n// after\nif (someObject is TItem typed) ((IList)collection).Insert(0, typed);","handlingStrategy":"type-guard","validationCode":"if (value is TItem) ((IList)collection).Insert(index, value);","typeGuard":"bool IsExpectedElement(object value) => value is TItem;","tryCatchPattern":"try { ((IList)collection).Insert(index, value); } catch (ArgumentException) { /* value is not a TItem; reject or convert */ }","preventionTips":["Only pass TItem instances through the IList interface","Prefer the strongly typed generic API over IList","Guard reflection/late-bound calls with 'is TItem' checks"],"tags":["type-mismatch","wpf","ilist","argument-exception"],"backgroundTag":"type-mismatch","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"}