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

What it means

TransformCollection.Cast(object value) validates every element added to the collection. If the supplied object is not a Transform instance, it throws ArgumentException with SR.Collection_BadType naming the collection type, the offending value's type, and the expected type 'Transform'.

Solutions

  1. Ensure the object being added is actually a Transform (RotateTransform, TranslateTransform, etc.)
  2. Check with 'if (value is Transform t)' before Add/Insert
  3. Fix the resource key or binding so it resolves to a Transform

Example fix

// before
transformCollection.Add(myBrush); // wrong resource
// after
if (myResource is Transform t)
    transformCollection.Add(t);
Defensive patterns

Strategy: type-guard

Validate before calling

if (item is not Transform) throw new ArgumentException($"Expected Transform, got {item?.GetType().Name}");
collection.Add((Transform)item);

Type guard

static bool IsTransform(object o) => o is Transform;

Try / catch

try { collection.Add(item); }
catch (ArgumentException ex) when (ex.Message.Contains("Transform")) { /* log type error; fix resource/binding */ }

Prevention

When it happens

Trigger: Calling Add, Insert, or the IList indexer (all of which route through Cast) with an object that is not a Transform, e.g. transformCollection.Add(rotateBrush) where the value is a Brush, string, or other non-Transform object.

Common situations: XAML or resource-dictionary lookups returning the wrong type; generic helper APIs adding items from an object[]; mistyping a variable due to name similarity (e.g. grabbing a Brush instead of a RotateTransform).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformCollection.cs:518

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

        #endregion

        #region Private Helpers

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

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

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

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)