{"record":{"id":"872f1e03a57c8194","repo":"dotnet/wpf","slug":"sr-format-sr-collection-badtype-this-gettype-name-value-872f1e","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Material\")","messagePattern":"SR\\.Format\\(SR\\.Collection_BadType, this\\.GetType\\(\\)\\.Name, value\\.GetType\\(\\)\\.Name, \"Material\"\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/MaterialCollection.cs","lineNumber":516,"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 Material Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is Material))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Material\"));\n            }\n\n            return (Material) 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(Material value)\n        {\n            int index = AddWithoutFiringPublicEvents(value);\n\n            // AddAtWithoutFiringPublicEvents incremented the version\n\n            WritePostscript();\n\n            return index;\n        }","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/MaterialCollection.cs#L498-L534","documentation":"The private Cast(object value) helper validates items added through the non-generic IList.Add/Insert paths. If value is not an instance of Material, it throws ArgumentException(SR.Collection_BadType) naming the collection type, the offending value's type, and the expected type \"Material\". This keeps the strongly-typed collection safe when accessed via weakly-typed interfaces.","triggerScenarios":"Calling ((IList)materials).Add(someObject) or Insert(i, someObject) where someObject is not a Material (e.g. a string, a Brush, or a MaterialGroup typed as object).","commonSituations":"Reflection or XAML/shader code that builds collections from untyped data; porting code from ArrayList; UI toolkits passing heterogeneous object lists; passing a Brush where a Material was intended.","solutions":["Add only Material instances (DiffuseMaterial, SpecularMaterial, EmissiveMaterial, MaterialGroup).","Wrap the value in an appropriate Material, e.g. new DiffuseMaterial(brush) if you have a Brush.","Type-check before adding: if (value is Material m) ((IList)materials).Add(m);"],"exampleFix":"// before\n((IList)group.Materials).Add(myBrush);\n// after\n((IList)group.Materials).Add(new DiffuseMaterial(myBrush));","handlingStrategy":"type-guard","validationCode":"if (value is not Material) throw new ArgumentException($\"Expected Material, got {value?.GetType().Name}\");","typeGuard":"static bool IsMaterial(object value) => value is Material;","tryCatchPattern":"try { ((IList)materials).Add(value); }\ncatch (ArgumentException ex) { /* value was not a Material */ }","preventionTips":["Prefer the generic Add(Material) over the non-generic IList.Add","Pattern-match with 'is Material m' before weakly-typed inserts","Convert Brushes to DiffuseMaterial explicitly instead of adding them raw"],"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"}