{"record":{"id":"57a6e87e46656113","repo":"dotnet/wpf","slug":"sr-collection-badtype","errorCode":null,"errorMessage":"SR.Collection_BadType","messagePattern":"SR\\.Collection_BadType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs","lineNumber":506,"sourceCode":"                DependencyObject inheritanceChild = _collection[i];\n                if (inheritanceChild != null && inheritanceChild.InheritanceContext == this)\n                {\n                    inheritanceChild.OnInheritanceContextChanged(args);\n                }\n            }\n        }\n\n        #endregion\n\n        #region Private Helpers\n\n        private PathSegment Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is PathSegment))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"PathSegment\"));\n            }\n\n            return (PathSegment) value;\n        }\n\n        // IList.Add returns int and IList<T>.Add does not. This\n        // is called by both Adds and IList<T>'s just ignores the\n        // integer\n        private int AddHelper(PathSegment value)\n        {\n            int index = AddWithoutFiringPublicEvents(value);\n\n            // AddAtWithoutFiringPublicEvents incremented the version\n\n            WritePostscript();\n\n            return index;\n        }","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs#L488-L524","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n((ICollection)segments).Add(new PathFigure());\n// after\nsegments.Add(new LineSegment(new Point(10,10), true));","handlingStrategy":"type-guard","validationCode":"if (value is not PathSegment) throw new ArgumentException($\"Expected PathSegment, got {value?.GetType().Name}\");","typeGuard":"static bool IsPathSegment(object v) => v is PathSegment;","tryCatchPattern":"try { segments.Insert(0, (PathSegment)value); }\ncatch (ArgumentException ex) { log(ex); /* reject or convert value */ }","preventionTips":["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)"],"tags":["wpf","collection","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}