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, "Vector")

What it means

VectorCollection.Cast (called from Add/Insert and other IList members) verifies every item added is a System.Windows.Vector. If the value is null-checked first and then fails the `is Vector` test, it throws ArgumentException with Collection_BadType, naming the collection type, the offending value's runtime type, and the expected element type 'Vector'. The collection is strongly typed and refuses to store anything that is not a Vector instance.

Solutions

  1. Ensure only Vector instances are passed to Add/Insert; convert Points with new Vector(p.X, p.Y) or p.ToVector().
  2. Before adding, check `if (value is Vector v)` and handle/skip otherwise instead of letting the collection throw.
  3. If building from doubles, construct Vector(x, y) explicitly rather than adding raw numbers.
  4. Check XAML/Binding output type; a binding producing Point or double into a Vector-typed collection property triggers this.

Example fix

// before
vectorCollection.Add(myPoint); // ArgumentException: is not a Vector
// after
vectorCollection.Add(new Vector(myPoint.X, myPoint.Y));
Defensive patterns

Strategy: validation

Validate before calling

if (value is not System.Windows.Vector)
    throw new ArgumentException($"{value?.GetType().Name ?? "null"} is not a Vector");
vectorCollection.Add(value);

Type guard

static bool IsVector(object value) => value is System.Windows.Vector;

Prevention

When it happens

Trigger: Calling vectorCollection.Add(value) or vectorCollection.Insert(index, value) (or setting via IList indexer) where value's runtime type is not Vector - e.g. adding a Point, double, string, or a boxed numeric value.

Common situations: Populating a VectorCollection from XAML with wrong element types; data-binding or code that conflates Point and Vector (both have X/Y doubles); passing parsed doubles or a PointCollection's items into a VectorCollection; reflection/generic code that adds items of object type without checking.

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/7b367dd00d271351. Report an issue: GitHub.

Appendix: source

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

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



        #endregion

        #region Private Helpers

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

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

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

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)