{"record":{"id":"238725e751d1c990","repo":"dotnet/wpf","slug":"sr-format-sr-collection-badtype-this-gettype-name-value-238725","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Transform\")","messagePattern":"SR\\.Format\\(SR\\.Collection_BadType, this\\.GetType\\(\\)\\.Name, value\\.GetType\\(\\)\\.Name, \"Transform\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformCollection.cs","lineNumber":518,"sourceCode":"                DependencyObject inheritanceChild = _collection[i];\n                if (inheritanceChild != null && inheritanceChild.InheritanceContext == this)\n                {\n                    inheritanceChild.OnInheritanceContextChanged(args);\n                }\n            }\n        }\n\n        #endregion\n\n        #region Private Helpers\n\n        private Transform Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is Transform))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Transform\"));\n            }\n\n            return (Transform) value;\n        }\n\n        // IList.Add returns int and IList<T>.Add does not. This\n        // is called by both Adds and IList<T>'s just ignores the\n        // integer\n        private int AddHelper(Transform value)\n        {\n            int index = AddWithoutFiringPublicEvents(value);\n\n            // AddAtWithoutFiringPublicEvents incremented the version\n\n            WritePostscript();\n\n            return index;\n        }","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformCollection.cs#L500-L536","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Ensure the object being added is actually a Transform (RotateTransform, TranslateTransform, etc.)","Check with 'if (value is Transform t)' before Add/Insert","Fix the resource key or binding so it resolves to a Transform"],"exampleFix":"// before\ntransformCollection.Add(myBrush); // wrong resource\n// after\nif (myResource is Transform t)\n    transformCollection.Add(t);","handlingStrategy":"type-guard","validationCode":"if (item is not Transform) throw new ArgumentException($\"Expected Transform, got {item?.GetType().Name}\");\ncollection.Add((Transform)item);","typeGuard":"static bool IsTransform(object o) => o is Transform;","tryCatchPattern":"try { collection.Add(item); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Transform\")) { /* log type error; fix resource/binding */ }","preventionTips":["Use pattern matching (is Transform t) before adding object-typed values","Verify XAML resource keys resolve to Transform-derived types","Keep collections strongly typed (TransformCollection vs ArrayList-style helpers)"],"tags":["wpf","collection","type-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}