{"record":{"id":"31320db9d4b937a0","repo":"dotnet/wpf","slug":"sr-format-sr-collection-badtype-this-gettype-name-value-31320d","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"TextDecoration\")","messagePattern":"SR\\.Format\\(SR\\.Collection_BadType, this\\.GetType\\(\\)\\.Name, value\\.GetType\\(\\)\\.Name, \"TextDecoration\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs","lineNumber":497,"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 TextDecoration Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is TextDecoration))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"TextDecoration\"));\n            }\n\n            return (TextDecoration) 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(TextDecoration value)\n        {\n            int index = AddWithoutFiringPublicEvents(value);\n\n            // AddAtWithoutFiringPublicEvents incremented the version\n\n            WritePostscript();\n\n            return index;\n        }","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs#L479-L515","documentation":"The private Cast helper validates every item passed to Add/Insert (non-generic IList paths) and throws ArgumentException naming the collection type, the offending value's type, and the expected 'TextDecoration'. It only accepts actual TextDecoration instances; subclasses are fine, unrelated types are not.","triggerScenarios":"Calling (IList)collection.Add(someNonTextDecoration) or Insert(i, obj) with a wrong-typed object, e.g. adding a TextEffect or string to the collection via the non-generic IList interface.","commonSituations":"Reflection or late-bound code adding generic objects; data binding supplying incompatible items; converting code from TextEffectCollection to TextDecorationCollection without updating item types.","solutions":["Only add TextDecoration instances (e.g. new TextDecoration() or TextDecorations.Underline[0])","Check 'value is TextDecoration' before the non-generic Add/Insert call","If you have a TextEffect, map/convert it to a TextDecoration first"],"exampleFix":"// before\nIList list = collection;\nlist.Add(myTextEffect);\n// after\nIList list = collection;\nif (myTextEffect is TextDecoration td) list.Add(td);","handlingStrategy":"type-guard","validationCode":"if (value == null) throw new ArgumentNullException(nameof(value));\nif (!(value is TextDecoration)) throw new ArgumentException($\"Expected TextDecoration, got {value.GetType().Name}\");","typeGuard":"bool IsTextDecoration(object value) => value is TextDecoration;","tryCatchPattern":null,"preventionTips":["Only pass TextDecoration instances through non-generic IList paths","Pattern-match ('is TextDecoration') before boxing into IList.Add","Watch for TextEffect vs TextDecoration mix-ups when porting code"],"tags":["wpf","argumentexception","type-mismatch","ilist"],"backgroundTag":"type-mismatch","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"}