{"record":{"id":"a56bd05f86dff274","repo":"dotnet/wpf","slug":"sr-format-sr-collection-badtype-this-gettype-name-value","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"T\")","messagePattern":"SR\\.Format\\(SR\\.Collection_BadType, this\\.GetType\\(\\)\\.Name, value\\.GetType\\(\\)\\.Name, \"T\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs","lineNumber":639,"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 T Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is T))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"T\"));\n            }\n\n            return (T) value;\n        }\n\n        // Extracts the count for the given IEnumerable<T> by sniffing for the\n        // ICollection and ICollection<T> interfaces.  If the count can not be\n        // extract it return -1.\n        private int GetCount(IEnumerable<T> enumerable)\n        {\n            ICollection collectionAsICollection = enumerable as ICollection;\n\n            if (collectionAsICollection != null)\n            {\n                return collectionAsICollection.Count;\n            }\n\n            ICollection<T> enumerableAsICollectionT = enumerable as ICollection<T>;","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs#L621-L657","documentation":"FreezableCollection<T>.Cast validates that every item added to the collection is an instance of T, throwing ArgumentException when value is not assignable to T. Because WPF FreezableCollection is often used via non-generic entry points (Add(object) from IList, Insert), a wrong item type must be rejected explicitly rather than crashing later on an unboxing/generic cast.","triggerScenarios":"Calling Add/Insert (directly or through IList.Add/IList.Insert) with an object that does not implement/derive from T, e.g. adding a string to a FreezableCollection<Geometry>, or adding a non-frozen/non-derived DependencyObject.","commonSituations":"XAML/parser-driven population with mistyped elements, reflection-based or data-binding code inserting items of the wrong runtime type, refactoring that changed T without updating item creation code.","solutions":["Ensure the item is of type T (or derived) before adding: construct the correct element type.","Check with 'if (item is T t) collection.Add(t);' before the call.","If heterogeneous content is needed, change the collection to FreezableCollection<T> with a common base type T that all items share."],"exampleFix":"// before\ncollection.Add(someString); // T is Geometry\n// after\nif (someString is Geometry g) collection.Add(g); else throw new ArgumentException(nameof(someString));","handlingStrategy":"type-guard","validationCode":"if (value == null) throw new ArgumentNullException(nameof(value));\nif (value is not T) throw new ArgumentException($\"{value.GetType().Name} is not {typeof(T).Name}\");","typeGuard":"static bool IsUsableItem(object value) => value is T;","tryCatchPattern":"try { collection.Add(value); }\ncatch (ArgumentException ex) { log.Warn($\"Rejected non-{typeof(T).Name} item: {value?.GetType().Name}\"); }","preventionTips":["Guard with 'is T' before Add/Insert via non-generic paths","Keep T a common base type of all items you plan to store","Use generic collection APIs to get compile-time checking"],"tags":["wpf","collection","argument","type-check"],"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"}