{"record":{"id":"029a28a080514701","repo":"dotnet/wpf","slug":"sr-collection-badtype-texteffectcollection","errorCode":null,"errorMessage":"SR.Collection_BadType","messagePattern":"SR\\.Collection_BadType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TextEffectCollection.cs","lineNumber":506,"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 TextEffect Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is TextEffect))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"TextEffect\"));\n            }\n\n            return (TextEffect) 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(TextEffect value)\n        {\n            int index = AddWithoutFiringPublicEvents(value);\n\n            // AddAtWithoutFiringPublicEvents incremented the version\n\n            WritePostscript();\n\n            return index;\n        }","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TextEffectCollection.cs#L488-L524","documentation":"TextEffectCollection.Cast rejects any value added to the collection that is not a TextEffect. The generated collection validates every item passed to Add/Insert (and via GeneralTransformCollection paths), throwing ArgumentException with Collection_BadType naming the collection, the offending value's type, and the expected type.","triggerScenarios":"Calling Add, Insert, or setting an IList indexer with an object that is not a TextEffect instance (e.g. a Transform or Drawing).","commonSituations":"Adding elements from a non-generic IList or ArrayList built from XAML-binding or data-driven sources; wrong resource resolved from a ResourceDictionary by key; refactors that changed the collection's element type.","solutions":["Ensure the value is a TextEffect (or derived) instance before adding","Cast or convert the item explicitly: if (x is TextEffect te) coll.Add(te);","Fix the source of the item — wrong dictionary key or wrong collection type in code"],"exampleFix":"// before\ncollection.Add(someDrawing); // Drawing, not TextEffect\n// after\nif (someDrawing is TextEffect effect)\n{\n    collection.Add(effect);\n}","handlingStrategy":"type-guard","validationCode":"if (value is not TextEffect) throw new ArgumentException($\"{value?.GetType().Name} is not a TextEffect\");","typeGuard":"static bool IsTextEffect(object o) => o is TextEffect;","tryCatchPattern":"try { collection.Add(item); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadType\") || ex.Message.Contains(\"must be of type\"))\n{ /* log wrong item type and skip */ }","preventionTips":["Only add items you obtained as TextEffect references","Use TryFindResource and pattern-match before adding","Prefer the generic IList<TextEffect>/ICollection<TextEffect> surface so the compiler enforces the type"],"tags":["wpf","collections","argumentexception","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}