dotnet/wpf · error · ArgumentException
SR.Format(SR.Collection_BadType, this.GetType().Name…
Error message
SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "Material")
What it means
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.
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);
Example fix
// before ((IList)group.Materials).Add(myBrush); // after ((IList)group.Materials).Add(new DiffuseMaterial(myBrush));
Defensive patterns
Strategy: type-guard
Validate before calling
if (value is not Material) throw new ArgumentException($"Expected Material, got {value?.GetType().Name}"); Type guard
static bool IsMaterial(object value) => value is Material;
Try / catch
try { ((IList)materials).Add(value); }
catch (ArgumentException ex) { /* value was not a Material */ } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.CannotConvertType
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadType
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/872f1e03a57c8194.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/MaterialCollection.cs:516
DependencyObject inheritanceChild = _collection[i];
if (inheritanceChild != null && inheritanceChild.InheritanceContext == this)
{
inheritanceChild.OnInheritanceContextChanged(args);
}
}
}
#endregion
#region Private Helpers
private Material Cast(object value)
{
ArgumentNullException.ThrowIfNull(value);
if (!(value is Material))
{
throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "Material"));
}
return (Material) value;
}
// IList.Add returns int and IList<T>.Add does not. This
// is called by both Adds and IList<T>'s just ignores the
// integer
private int AddHelper(Material value)
{
int index = AddWithoutFiringPublicEvents(value);
// AddAtWithoutFiringPublicEvents incremented the version
WritePostscript();
return index;
}View on GitHub (pinned to 81131a70a4)