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

What it means

Cast is the private gatekeeper for Add/Insert on GeneralTransform3DCollection. It throws ArgumentException (Collection_BadType) when the supplied object is not a GeneralTransform3D. Strongly-typed overloads make this nearly unreachable, but the ICollection/IList non-generic paths accept object and must reject wrong types explicitly.

Solutions

  1. Only insert instances of GeneralTransform3D (e.g. RotateTransform3D, TranslateTransform3D, MatrixTransform3D, Transform3DGroup).
  2. Filter before adding: if (item is GeneralTransform3D t) collection.Add(t);
  3. If converting from a 2D transform collection, wrap appropriately or build the intended 3D transforms instead of reusing 2D objects.

Example fix

// before
collection.Add((object)rotateTransform2D);
// after
if (rotateTransform2D is GeneralTransform3D)
    collection.Add(rotateTransform2D);
else
    throw new InvalidOperationException("Expected a 3D transform");
Defensive patterns

Strategy: type-guard

Validate before calling

// before adding via non-generic IList
if (item is GeneralTransform3D) ((IList)collection).Add(item);

Type guard

static bool IsValidItem(object o) => o is GeneralTransform3D;

Try / catch

try { ((IList)collection).Add(candidate); }
catch (ArgumentException ex) when (ex.ParamName == null && ex.Message.Contains("GeneralTransform3D"))
{
    // log and skip the invalid item
}

Prevention

When it happens

Trigger: Calling ((ICollection)collection).Add(someObject) or ((IList)collection).Insert(i, obj) where obj is not a GeneralTransform3D instance; also Add(object) path used by data binding or XAML with a wrong element.

Common situations: XAML mistakenly placing a GeneralTransform2D-type resource or a Material into a GeneralTransform3DCollection; reflection-based population code; databinding a mixed list to the collection.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/GeneralTransform3DCollection.cs:504

                DependencyObject inheritanceChild = _collection[i];
                if (inheritanceChild != null && inheritanceChild.InheritanceContext == this)
                {
                    inheritanceChild.OnInheritanceContextChanged(args);
                }
            }
        }

        #endregion

        #region Private Helpers

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

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

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

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)