dotnet/wpf · error · ArgumentException

SR.Collection_BadType

Error message

SR.Collection_BadType

What it means

PointCollection.Cast(object) validates that any value added to the collection is a System.Windows.Point. If the value is null it throws ArgumentNullException; if it is not a Point it throws ArgumentException with Collection_BadType, reporting the collection type, the offending value's type, and the expected type 'Point'. This guards the non-generic IList surface (Add/Insert) where the compiler cannot enforce the element type.

Solutions

  1. Pass an actual Point instance: points.Add(new Point(x, y));
  2. If you have a string, parse it first with Point.Parse(s) (or TypeConverter) before adding.
  3. Cast/convert the value to Point before calling the non-generic Add/Insert, or use PointCollection.Add(Point) so the compiler enforces the type.

Example fix

// before
((IList)points).Add("10,20"); // ArgumentException Collection_BadType
// after
((IList)points).Add(Point.Parse("10,20"));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not Point) throw new ArgumentException($"Expected Point, got {value?.GetType().Name}");

Type guard

static bool IsPoint(object v) => v is Point;

Try / catch

try { ((IList)points).Add(value); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadType")) { /* convert or reject value */ }

Prevention

When it happens

Trigger: Calling ((IList)pointCollection).Add(someObject) or .Insert(index, someObject) with an object that is not a Point — e.g. adding a Point3D, a string like "1,2", a float, or a boxed value of the wrong type.

Common situations: Data binding or reflection-driven code that adds items to the non-generic IList interface; parsing code that produces strings instead of parsed Points; XAML-adjacent tooling passing variant objects; confusion between Point and Vector/Size structs.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7b69198a6e7eb7fd. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PointCollection.cs:455

        /// </summary>
        internal Point Internal_GetItem(int i)
        {
            return _collection[i];
        }



        #endregion

        #region Private Helpers

        private Point Cast(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            if (!(value is Point))
            {
                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "Point"));
            }

            return (Point) 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(Point value)
        {
            int index = AddWithoutFiringPublicEvents(value);

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)