dotnet/wpf · error · ArgumentException

SR.Collection_BadType

Error message

SR.Collection_BadType

What it means

TextEffectCollection.Cast rejects any value added to the collection that is not a TextEffect. The generated collection validates every item passed to Add/Insert (and via GeneralTransformCollection paths), throwing ArgumentException with Collection_BadType naming the collection, the offending value's type, and the expected type.

Solutions

  1. Ensure the value is a TextEffect (or derived) instance before adding
  2. Cast or convert the item explicitly: if (x is TextEffect te) coll.Add(te);
  3. Fix the source of the item — wrong dictionary key or wrong collection type in code

Example fix

// before
collection.Add(someDrawing); // Drawing, not TextEffect
// after
if (someDrawing is TextEffect effect)
{
    collection.Add(effect);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not TextEffect) throw new ArgumentException($"{value?.GetType().Name} is not a TextEffect");

Type guard

static bool IsTextEffect(object o) => o is TextEffect;

Try / catch

try { collection.Add(item); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadType") || ex.Message.Contains("must be of type"))
{ /* log wrong item type and skip */ }

Prevention

When it happens

Trigger: Calling Add, Insert, or setting an IList indexer with an object that is not a TextEffect instance (e.g. a Transform or Drawing).

Common situations: Adding elements from a non-generic IList or ArrayList built from XAML-binding or data-driven sources; wrong resource resolved from a ResourceDictionary by key; refactors that changed the collection's element type.

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/029a28a080514701. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TextEffectCollection.cs:506

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

        #endregion

        #region Private Helpers

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

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

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

            // AddAtWithoutFiringPublicEvents incremented the version

            WritePostscript();

            return index;
        }

View on GitHub (pinned to 81131a70a4)