dotnet/wpf · error · ArgumentException

SR.Collection_BadType (GeneralTransform)

Error message

SR.Collection_BadType (GeneralTransform)

What it means

Thrown by the private Cast method (used by Add, Insert, and the collection constructor) when the value being added to a GeneralTransformCollection is not a GeneralTransform. The message names the collection type, the offending value's type, and the expected type 'GeneralTransform'.

Solutions

  1. Only add objects deriving from GeneralTransform (e.g. TranslateTransform, RotateTransform, TransformGroup)
  2. Check the object's runtime type with a type guard before Add/Insert
  3. In XAML, verify child elements are transform types and namespaces are correct

Example fix

// before
collection.Add(myVisualBrush); // wrong type
// after
if (myVisualBrush is GeneralTransform t) { collection.Add(t); }
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value is GeneralTransform)) throw new ArgumentException($"Expected GeneralTransform, got {value.GetType().Name}");

Type guard

static bool IsGeneralTransform(object o) => o is GeneralTransform;

Try / catch

try { collection.Add(value); } catch (ArgumentException) { /* value is not a GeneralTransform — handle or log */ }

Prevention

When it happens

Trigger: Calling collection.Add(someObject) or collection.Insert(i, someObject) where someObject is not derived from GeneralTransform — e.g. adding a string, a null-cast object, or a misconfigured XAML resource.

Common situations: XAML content mistakes (adding non-transform elements under <TransformGroup> or a GeneralTransformCollection resource), data binding producing the wrong type, or refactors where the wrong collection variable is used.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/GeneralTransformCollection.cs:506

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

        #endregion

        #region Private Helpers

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

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

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

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)