dotnet/wpf · error
SR.Format(SR.TableCollectionElementTypeExpected…
Error message
SR.Format(SR.TableCollectionElementTypeExpected, typeof(TItem).Name)
What it means
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[].
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
Example fix
// before ((IList)collection).Insert(0, someObject); // after if (someObject is TItem typed) ((IList)collection).Insert(0, typed);
Defensive patterns
Strategy: type-guard
Validate before calling
if (value is TItem) ((IList)collection).Insert(index, value);
Type guard
bool IsExpectedElement(object value) => value is TItem;
Try / catch
try { ((IList)collection).Insert(index, value); } catch (ArgumentException) { /* value is not a TItem; reject or convert */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.BadFixedTextPosition
- SR.Collection_BadType (double)
- SR.Collection_BadType
- SR.Collection_BadType
- SR.Collection_BadType
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7f8a1c1be17bf38a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs:455
TItem item = value as TItem;
if (item == null)
{
return -1;
}
return this.IndexOf(item);
}
void IList.Insert(int index, object value)
{
ArgumentNullException.ThrowIfNull(value);
TItem newItem = value as TItem;
if (newItem == null)
{
throw new ArgumentException(SR.Format(SR.TableCollectionElementTypeExpected, typeof(TItem).Name), nameof(value));
}
this.Insert(index, newItem);
}
bool IList.IsFixedSize
{
get
{
return false;
}
}
bool IList.IsReadOnly
{
get
{
return this.IsReadOnly;View on GitHub (pinned to 81131a70a4)