{"record":{"id":"a6911155f1f1363d","repo":"dotnet/wpf","slug":"sr-collection-badtype-drawing","errorCode":null,"errorMessage":"SR.Collection_BadType (Drawing)","messagePattern":"SR\\.Collection_BadType \\(Drawing\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.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 Drawing Cast(object value)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (!(value is Drawing))\n            {\n                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, \"Drawing\"));\n            }\n\n            return (Drawing) 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(Drawing 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/DrawingCollection.cs#L500-L536","documentation":"DrawingCollection.Cast (called from Add/Insert) rejects any value that is not a Drawing instance, throwing ArgumentException with SR.Collection_BadType naming the collection type, the value's actual type, and the expected type. WPF freezable collections are strongly typed even where object-based IList APIs let weakly-typed values slip in.","triggerScenarios":"Calling DrawingCollection.Add(object), Insert(int, object), or the IList indexer setter with a non-Drawing object (e.g. an Image, Brush, Geometry, or a custom element that is not a Drawing subclass).","commonSituations":"Mixing up WPF graphics types: adding a Brush or Geometry where a Drawing is expected; using the non-generic IList interface from reflection, XAML markup extensions, or databinding code that boxes values; porting code from DrawingContext calls to collection Adds.","solutions":["Wrap the object in a Drawing-derived type first, e.g. use a GeometryDrawing/ ImageDrawing that references the Brush or ImageSource you meant to add.","Check the runtime type: the value must derive from System.Windows.Media.Drawing; fix the variable's declared type or the code that constructs it.","If using the IList (non-generic) API, cast or validate with `value is Drawing` before Add/Insert."],"exampleFix":"// before\ndrawingCollection.Add(myBrush); // ArgumentException\n// after\ndrawingCollection.Add(new GeometryDrawing { Brush = myBrush, Geometry = myGeometry });","handlingStrategy":"type-guard","validationCode":"if (value is not System.Windows.Media.Drawing d) throw new ArgumentException($\"Expected Drawing, got {value?.GetType().Name}\"); collection.Add(d);","typeGuard":"static bool IsDrawing(object v) => v is System.Windows.Media.Drawing;","tryCatchPattern":"try { collection.Add(value); } catch (ArgumentException ex) when (ex.Message.Contains(\"Drawing\")) { /* log type mismatch, substitute correct Drawing */ }","preventionTips":["Use the generic ICollection<Drawing> surface, not IList, so the compiler enforces item types","Remember brushes/geometries are not Drawings; wrap them in GeometryDrawing/ImageDrawing","Enable nullable reference types so wrong-typed variables surface at compile time"],"tags":["wpf","type-mismatch","collections"],"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"}