dotnet/wpf · error · ArgumentException

SR.Collection_BadType

Error message

SR.Collection_BadType

What it means

Point3DCollection's private Cast(object) helper (used by IList.Add and IList.Insert) throws ArgumentException(SR.Collection_BadType) when the boxed value is not a Point3D. The non-generic IList surface only accepts Point3D instances.

Solutions

  1. Only add Point3D instances via the non-generic IList interface
  2. Use the strongly typed Add(Point3D) method so type errors surface at compile time
  3. Convert/construct a Point3D from the value before inserting

Example fix

// before
((IList)collection).Add(new Vector3D(1, 2, 3)); // throws
// after
collection.Add(new Point3D(1, 2, 3));
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

bool IsPoint3D(object v) => v is Point3D;

Try / catch

try { ((IList)col).Add(value); }
catch (ArgumentException) { /* value was not Point3D; convert or reject */ }

Prevention

When it happens

Trigger: ((IList)point3DCollection).Add(someVector3D) or .Insert(0, otherType) where the object is not a Point3D; null is rejected separately with ArgumentNullException.

Common situations: Adding values parsed from XML/JSON into the non-generic IList; generic math code that mixes Point3D and Vector3D; reflection-driven population of collections.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Point3DCollection.cs:453

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



        #endregion

        #region Private Helpers

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

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

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

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)