{"record":{"id":"e5f9314c7e3a6218","repo":"dotnet/wpf","slug":"sr-positionalargumentswronglength","errorCode":null,"errorMessage":"SR.PositionalArgumentsWrongLength","messagePattern":"SR\\.PositionalArgumentsWrongLength","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfKnownTypeInvoker.cs","lineNumber":29,"sourceCode":"\n        public WpfKnownTypeInvoker(WpfKnownType type)\n            : base(type)\n        {\n            _type = type;\n        }\n\n        public override object CreateInstance(object[] arguments)\n        {\n            if ((arguments == null || arguments.Length == 0) && _type.DefaultConstructor != null)\n            {\n                return _type.DefaultConstructor.Invoke();\n            }\n            else if (_type.IsMarkupExtension)\n            {\n                Baml6ConstructorInfo ctorInfo;\n                if(!_type.Constructors.TryGetValue(arguments.Length, out ctorInfo))\n                {\n                    throw new InvalidOperationException(SR.PositionalArgumentsWrongLength);\n                }\n                return ctorInfo.Constructor(arguments);\n            }\n            else\n            {\n                return base.CreateInstance(arguments);\n            }\n        }\n    }\n}\n","sourceCodeStart":11,"sourceCodeEnd":40,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfKnownTypeInvoker.cs#L11-L40","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the MarkupExtension type has a constructor matching the number of arguments recorded in the BAML.","Recompile the XAML/BAML against the current version of the assembly defining the extension.","Call CreateInstance with the exact argument count of an existing public constructor.","If authoring the extension, add a constructor overload with the required arity."],"exampleFix":"// before\nnew WpfKnownTypeInvoker(knownType).CreateInstance(new object[] { \"a\", \"b\" }); // type only has a 1-arg ctor\n// after\nnew WpfKnownTypeInvoker(knownType).CreateInstance(new object[] { \"a\" }); // matches existing ctor arity","handlingStrategy":"validation","validationCode":"if (!_markupExtensionType.GetConstructors().Any(c => c.GetParameters().Length == args.Length))\n    throw new ArgumentException($\"No constructor on {_markupExtensionType} takes {args.Length} arguments.\");","typeGuard":null,"tryCatchPattern":"try { ext = invoker.CreateInstance(args); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"PositionalArguments\") || ex.Message.Contains(\"arguments\"))\n{\n    // fall back: construct with default ctor and set properties\n}","preventionTips":["Keep MarkupExtension constructors stable across assembly versions.","Recompile BAML whenever extension constructor signatures change.","Provide a parameterless constructor plus settable properties as a fallback."],"tags":["wpf","baml","xaml","markup-extension","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}