dotnet/wpf · error · ArgumentException
SR.Format(SR.Collection_BadType, this.GetType().Name…
Error message
SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "T")
What it means
FreezableCollection<T>.Cast validates that every item added to the collection is an instance of T, throwing ArgumentException when value is not assignable to T. Because WPF FreezableCollection is often used via non-generic entry points (Add(object) from IList, Insert), a wrong item type must be rejected explicitly rather than crashing later on an unboxing/generic cast.
Solutions
- Ensure the item is of type T (or derived) before adding: construct the correct element type.
- Check with 'if (item is T t) collection.Add(t);' before the call.
- If heterogeneous content is needed, change the collection to FreezableCollection<T> with a common base type T that all items share.
Example fix
// before collection.Add(someString); // T is Geometry // after if (someString is Geometry g) collection.Add(g); else throw new ArgumentException(nameof(someString));
Defensive patterns
Strategy: type-guard
Validate before calling
if (value == null) throw new ArgumentNullException(nameof(value));
if (value is not T) throw new ArgumentException($"{value.GetType().Name} is not {typeof(T).Name}"); Type guard
static bool IsUsableItem(object value) => value is T;
Try / catch
try { collection.Add(value); }
catch (ArgumentException ex) { log.Warn($"Rejected non-{typeof(T).Name} item: {value?.GetType().Name}"); } Prevention
- Guard with 'is T' before Add/Insert via non-generic paths
- Keep T a common base type of all items you plan to store
- Use generic collection APIs to get compile-time checking
When it happens
Trigger: Calling Add/Insert (directly or through IList.Add/IList.Insert) with an object that does not implement/derive from T, e.g. adding a string to a FreezableCollection<Geometry>, or adding a non-frozen/non-derived DependencyObject.
Common situations: XAML/parser-driven population with mistyped elements, reflection-based or data-binding code inserting items of the wrong runtime type, refactoring that changed T without updating item creation code.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Cannot pass multidimensional array to the CopyTo method on…
- SR.Format(SR.TextElementCollection_PreviousSiblingDoesNotBel…
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ID is not a valid XSD ID.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a56bd05f86dff274.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs:639
DependencyObject inheritanceChild = _collection[i];
if (inheritanceChild!= null && inheritanceChild.InheritanceContext == this)
{
inheritanceChild.OnInheritanceContextChanged(args);
}
}
}
#endregion
#region Private Helpers
private T Cast(object value)
{
ArgumentNullException.ThrowIfNull(value);
if (!(value is T))
{
throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "T"));
}
return (T) value;
}
// Extracts the count for the given IEnumerable<T> by sniffing for the
// ICollection and ICollection<T> interfaces. If the count can not be
// extract it return -1.
private int GetCount(IEnumerable<T> enumerable)
{
ICollection collectionAsICollection = enumerable as ICollection;
if (collectionAsICollection != null)
{
return collectionAsICollection.Count;
}
ICollection<T> enumerableAsICollectionT = enumerable as ICollection<T>;View on GitHub (pinned to 81131a70a4)