dotnet/wpf · error · ArgumentException
SR.Collection_BadType
Error message
SR.Collection_BadType
What it means
PathSegmentCollection.Cast throws Collection_BadType when a value added to the collection is not a PathSegment. The generated collection validates every Add/Insert argument and rejects any object whose runtime type is not PathSegment, naming the expected type in the message.
Solutions
- Only pass objects deriving from System.Windows.Media.PathSegment (LineSegment, BezierSegment, PolyLineSegment, etc.).
- Verify you are using the correct collection type for your element (PathFigureCollection for PathFigures).
- Check the value at runtime with `is PathSegment` before adding.
- If working through IList, cast/validate the element explicitly before Add/Insert.
Example fix
// before ((ICollection)segments).Add(new PathFigure()); // after segments.Add(new LineSegment(new Point(10,10), true));
Defensive patterns
Strategy: type-guard
Validate before calling
if (value is not PathSegment) throw new ArgumentException($"Expected PathSegment, got {value?.GetType().Name}"); Type guard
static bool IsPathSegment(object v) => v is PathSegment;
Try / catch
try { segments.Insert(0, (PathSegment)value); }
catch (ArgumentException ex) { log(ex); /* reject or convert value */ } Prevention
- Use the generic ICollection<PathSegment>/IList<PathSegment> surface so the compiler enforces element type
- Check `is PathSegment` before adding through non-generic IList
- Verify you are on the right collection class (PathSegmentCollection vs PathFigureCollection)
When it happens
Trigger: Calling Add, Insert (or the IList.Add path) on a PathSegmentCollection with an object that is not a PathSegment — e.g. adding a PathFigure, Point, or raw geometry object.
Common situations: Mistyping which WPF media collection to add to (PathFigureCollection vs PathSegmentCollection), boxing value types into an IList-typed reference, or data binding/serialization producing wrong element types.
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.CannotConvertType
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadType (double)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/57a6e87e46656113.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs:506
DependencyObject inheritanceChild = _collection[i];
if (inheritanceChild != null && inheritanceChild.InheritanceContext == this)
{
inheritanceChild.OnInheritanceContextChanged(args);
}
}
}
#endregion
#region Private Helpers
private PathSegment Cast(object value)
{
ArgumentNullException.ThrowIfNull(value);
if (!(value is PathSegment))
{
throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "PathSegment"));
}
return (PathSegment) value;
}
// IList.Add returns int and IList<T>.Add does not. This
// is called by both Adds and IList<T>'s just ignores the
// integer
private int AddHelper(PathSegment value)
{
int index = AddWithoutFiringPublicEvents(value);
// AddAtWithoutFiringPublicEvents incremented the version
WritePostscript();
return index;
}View on GitHub (pinned to 81131a70a4)