dotnet/wpf · error · ArgumentException

SR.Format(SR.UnexpectedParameterType, value.GetType()…

Error message

SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(NamedElement))

What it means

This AddChild implementation (ListElement-style container) accepts only NamedElement children and throws ArgumentException with UnexpectedParameterType otherwise. The generic resource echoes the provided value's type plus the expected NamedElement type.

Solutions

  1. Add only NamedElement instances here.
  2. Pass ListItemStructure children to ListStructure.AddChild instead.
  3. Use the typed Add(NamedElement) API to get compile-time safety.

Example fix

// before
listElement.AddChild(new ListItemStructure());
// after
parentListStructure.AddChild(new ListItemStructure());
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not NamedElement) throw new ArgumentException($"Expected NamedElement, got {value.GetType()}.");

Type guard

bool IsNamedElement(object v) => v is NamedElement;

Try / catch

try { el.AddChild(value); } catch (ArgumentException) { /* add ListItems to ListStructure instead */ }

Prevention

When it happens

Trigger: Calling AddChild with a ListItemStructure or any non-NamedElement object on this container; XAML schemas where the parent expects only named leaf elements.

Common situations: Building ListStructure trees where the author confused the levels — adding ListItemStructure where only named marker/leaf elements belong, or vice versa.

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/96e995fbe566b94e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentStructures/SemanticBasicElement.cs:152

    /// </summary>
    public class FigureStructure : SemanticBasicElement, IAddChild, IEnumerable<NamedElement>, IEnumerable
    {
        /// <summary>
        ///
        /// </summary>
        public FigureStructure()
        {
            _elementType = FixedElement.ElementType.Figure;
        }

        void IAddChild.AddChild(object value)
        {
            if (value is NamedElement)
            {
                _elementList.Add((BlockElement)value);
                return;
            }
            throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(NamedElement)), nameof(value));
        }
        
        void IAddChild.AddText(string text) { }
        
        public void Add(NamedElement element)
        {
            ArgumentNullException.ThrowIfNull(element);
            ((IAddChild) this).AddChild(element);
        }
        
        IEnumerator<NamedElement> IEnumerable<NamedElement>.GetEnumerator()
        {
            throw new NotSupportedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<NamedElement>)this).GetEnumerator();

View on GitHub (pinned to 81131a70a4)