dotnet/wpf · error · XamlXmlWriterException
SR.ConstructorNotFoundForGivenPositionalParameters
Error message
SR.ConstructorNotFoundForGivenPositionalParameters
What it means
Positional-parameter expansion matches the supplied argument count to properties decorated with [ConstructorArgument]. If the number of constructor parameters on the CLR type differs from the number of ConstructorArgument properties, no matching constructor exists and XamlXmlWriterException is thrown.
Solutions
- Fix the markup extension type so every constructor parameter has a matching [ConstructorArgument] property (same count).
- Remove stale or extra [ConstructorArgument] attributes from properties.
- Use a constructor overload whose parameters exactly correspond to the attributed properties.
- Catch XamlXmlWriterException and emit explicit x:Arguments nodes instead of relying on expansion.
Example fix
// before
public Foo(string a) { ... }
[ConstructorArgument("a")] public string A { get; }
[ConstructorArgument("b")] public string B { get; } // extra CAA, no ctor param b
// after
[ConstructorArgument("a")] public string A { get; } // only params that exist on ctor Defensive patterns
Strategy: validation
Validate before calling
var caaProps = GetAllPropertiesWithCAA(type); if (caaProps.Count != ctorParams.Length) throw new InvalidOperationException("ConstructorArgument property count must match constructor parameter count"); Try / catch
try { writer.WriteEndObject(); } catch (XamlXmlWriterException ex) when (ex.Message.Contains("ConstructorNotFound")) { /* emit x:Arguments manually */ } Prevention
- Keep [ConstructorArgument] attributes in sync with constructors.
- Test every markup extension round-trip through XamlXmlWriter.
- Avoid overloads that lack full ConstructorArgument coverage.
When it happens
Trigger: Expanding positional parameters where GetParametersInfo(objectXamlType, n) returns a constructor with m parameters but GetAllPropertiesWithCAA finds a different count of [ConstructorArgument]-attributed properties (count mismatch).
Common situations: Markup extension types whose [ConstructorArgument] attributes were added/removed out of sync with the constructor, multiple constructors confusing parameter resolution, third-party extensions with inconsistent metadata.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- ArgumentNullException(nameof(arrayType))
- ArgumentNullException(nameof(assemblyNames))
- ArgumentNullException(nameof(contentType))
- ArgumentNullException(nameof(loaderType))
- ArgumentNullException(nameof(member))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9669cd6fb00e0576.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:1923
XamlType objectXamlType = frame.Type;
Debug.Assert(objectXamlType is not null);
Type objectClrType = objectXamlType.UnderlyingType;
if (objectClrType is null)
{
throw new XamlXmlWriterException(SR.Format(
SR.ExpandPositionalParametersWithoutUnderlyingType, objectXamlType.GetQualifiedName()));
}
int numOfParameters = writer.ppStateInfo.NodesList.Count;
ParameterInfo[] constructorParameters = GetParametersInfo(objectXamlType, numOfParameters);
List<XamlMember> ctorArgProps = GetAllPropertiesWithCAA(objectXamlType);
// If there aren't the same number of parameters then we throw
if (constructorParameters.Length != ctorArgProps.Count)
{
throw new XamlXmlWriterException(SR.ConstructorNotFoundForGivenPositionalParameters);
}
for (int i = 0; i < constructorParameters.Length; i++)
{
ParameterInfo paraminfo = constructorParameters[i];
XamlMember matchingProperty = null;
foreach (var potentialProperty in ctorArgProps)
{
if ((potentialProperty.Type.UnderlyingType == paraminfo.ParameterType) &&
(XamlObjectReader.GetConstructorArgument(potentialProperty) == paraminfo.Name))
{
matchingProperty = potentialProperty;
break;
}
}
if (matchingProperty is null)View on GitHub (pinned to 81131a70a4)