{"record":{"id":"9b256f884c8c85a6","repo":"dotnet/wpf","slug":"sr-expandpositionalparametersintypewithnodefaultconstructor","errorCode":null,"errorMessage":"SR.ExpandPositionalParametersinTypeWithNoDefaultConstructor","messagePattern":"SR\\.ExpandPositionalParametersinTypeWithNoDefaultConstructor","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1181,"sourceCode":"                writer.namespaceScopes.Push(new Frame\n                {\n                    AllocatingNodeType = XamlNodeType.StartMember,\n                    Type = writer.namespaceScopes.Peek().Type,\n                    Member = property\n                });\n\n                XamlType containingType = writer.namespaceScopes.Peek().Type;\n                if (property == XamlLanguage.PositionalParameters)\n                {\n                    // the writer is not in a state where it can write markup extensions in curly form\n                    // so it expands the positional parameters as properties.\n\n                    writer.namespaceScopes.Pop();\n                    // but in order to expand properties, the markup extension needs to have a default constructor\n\n                    if (containingType is not null && containingType.ConstructionRequiresArguments)\n                    {\n                        throw new XamlXmlWriterException(SR.ExpandPositionalParametersinTypeWithNoDefaultConstructor);\n                    }\n\n                    writer.ppStateInfo.ReturnState = State;\n                    writer.currentState = ExpandPositionalParameters.State;\n                }\n                else if (IsImplicit(property))\n                {\n                    // Stop trying attributes\n\n                    writer.WriteDeferredNamespaces(XamlNodeType.StartObject);\n                    writer.currentState = InMember.State;\n                }\n                else if (property == containingType.ContentProperty)\n                {\n                    writer.currentState = TryContentPropertyInTryAttributesState.State;\n                }\n                else if (property.IsDirective && (property.Type is not null && (property.Type.IsCollection || property.Type.IsDictionary)))\n                {","sourceCodeStart":1163,"sourceCodeEnd":1199,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1163-L1199","documentation":"Same failure mode as the positional-parameter expansion guard but reached from the property-expansion path: when expanding a markup extension written with property syntax (or implicit members) XamlXmlWriter needs to instantiate the extension type, and if containingType.ConstructionRequiresArguments is true (no default constructor) it throws XamlXmlWriterException ExpandPositionalParametersinTypeWithNoDefaultConstructor at this site.","triggerScenarios":"Writing a markup extension with expansion required (property-element expansion path at line ~1181) where the containing markup-extension XamlType lacks a default constructor (ConstructionRequiresArguments true).","commonSituations":"Custom MarkupExtension classes without parameterless constructors; refactors that added required constructor arguments; serialization of extensions used in templates/styles that forces expansion.","solutions":["Add a public parameterless constructor to the markup extension class","Serialize the extension with attribute/explicit property syntax that avoids the expansion path","Provide a custom XamlValueConverter/type mapping so the writer does not need to instantiate the type"],"exampleFix":"// before\npublic class MyExt : MarkupExtension { public MyExt(int n) {...} } // throws on expansion\n// after\npublic class MyExt : MarkupExtension {\n  public MyExt() {}\n  public MyExt(int n) {...}\n}","handlingStrategy":"validation","validationCode":"if (extType != null && extType.ConstructionRequiresArguments && expansionNeeded) throw new InvalidOperationException(\"Markup extension must have a default constructor for expansion\");","typeGuard":"bool CanExpandMarkupExtension(XamlType t) => t == null || !t.ConstructionRequiresArguments;","tryCatchPattern":"try { writer.WriteNode(reader); }\ncatch (XamlXmlWriterException ex) when (ex.Message.Contains(\"DefaultConstructor\")) { /* serialize with named properties instead */ }","preventionTips":["Always include a parameterless constructor on MarkupExtension subclasses","Prefer settable properties over required constructor arguments in serializable extensions","Smoke-test save/load round-trips for every custom extension"],"tags":["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-22T01:17:13.364Z"}