dotnet/wpf · error · InvalidOperationException

SR.PositionalArgumentsWrongLength

Error message

SR.PositionalArgumentsWrongLength

What it means

WpfKnownTypeInvoker.CreateInstance creates a MarkupExtension from BAML positional arguments. For markup extensions the number of constructor arguments must exactly match one of the registered Baml6ConstructorInfo entries; if no constructor with that arity exists, this InvalidOperationException with SR.PositionalArgumentsWrongLength is thrown.

Solutions

  1. Ensure the MarkupExtension type has a constructor matching the number of arguments recorded in the BAML.
  2. Recompile the XAML/BAML against the current version of the assembly defining the extension.
  3. Call CreateInstance with the exact argument count of an existing public constructor.
  4. If authoring the extension, add a constructor overload with the required arity.

Example fix

// before
new WpfKnownTypeInvoker(knownType).CreateInstance(new object[] { "a", "b" }); // type only has a 1-arg ctor
// after
new WpfKnownTypeInvoker(knownType).CreateInstance(new object[] { "a" }); // matches existing ctor arity
Defensive patterns

Strategy: validation

Validate before calling

if (!_markupExtensionType.GetConstructors().Any(c => c.GetParameters().Length == args.Length))
    throw new ArgumentException($"No constructor on {_markupExtensionType} takes {args.Length} arguments.");

Try / catch

try { ext = invoker.CreateInstance(args); }
catch (InvalidOperationException ex) when (ex.Message.Contains("PositionalArguments") || ex.Message.Contains("arguments"))
{
    // fall back: construct with default ctor and set properties
}

Prevention

When it happens

Trigger: Calling CreateInstance with an arguments array whose length has no matching constructor on the markup extension type — e.g. a compiled-BAML extension whose constructors changed or whose BAML recorded an argument count that no longer exists.

Common situations: Mismatch between compiled BAML and a re-versioned control library where a MarkupExtension constructor signature changed; hand-written code invoking the invoker directly with the wrong number of args; template/BAML produced by an older markup compiler against newer types.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfKnownTypeInvoker.cs:29

        public WpfKnownTypeInvoker(WpfKnownType type)
            : base(type)
        {
            _type = type;
        }

        public override object CreateInstance(object[] arguments)
        {
            if ((arguments == null || arguments.Length == 0) && _type.DefaultConstructor != null)
            {
                return _type.DefaultConstructor.Invoke();
            }
            else if (_type.IsMarkupExtension)
            {
                Baml6ConstructorInfo ctorInfo;
                if(!_type.Constructors.TryGetValue(arguments.Length, out ctorInfo))
                {
                    throw new InvalidOperationException(SR.PositionalArgumentsWrongLength);
                }
                return ctorInfo.Constructor(arguments);
            }
            else
            {
                return base.CreateInstance(arguments);
            }
        }
    }
}

View on GitHub (pinned to 81131a70a4)