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, "TextDecoration")

What it means

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.

Solutions

  1. Only add TextDecoration instances (e.g. new TextDecoration() or TextDecorations.Underline[0])
  2. Check 'value is TextDecoration' before the non-generic Add/Insert call
  3. If you have a TextEffect, map/convert it to a TextDecoration first

Example fix

// before
IList list = collection;
list.Add(myTextEffect);
// after
IList list = collection;
if (myTextEffect is TextDecoration td) list.Add(td);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value == null) throw new ArgumentNullException(nameof(value));
if (!(value is TextDecoration)) throw new ArgumentException($"Expected TextDecoration, got {value.GetType().Name}");

Type guard

bool IsTextDecoration(object value) => value is TextDecoration;

Prevention

When it happens

Trigger: 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.

Common situations: Reflection or late-bound code adding generic objects; data binding supplying incompatible items; converting code from TextEffectCollection to TextDecorationCollection without updating item types.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/31320db9d4b937a0. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs:497

                DependencyObject inheritanceChild = _collection[i];
                if (inheritanceChild != null && inheritanceChild.InheritanceContext == this)
                {
                    inheritanceChild.OnInheritanceContextChanged(args);
                }
            }
        }

        #endregion

        #region Private Helpers

        private TextDecoration Cast(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            if (!(value is TextDecoration))
            {
                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "TextDecoration"));
            }

            return (TextDecoration) 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(TextDecoration value)
        {
            int index = AddWithoutFiringPublicEvents(value);

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)