dotnet/wpf · error · XamlObjectReaderException
SR.ObjectReaderNoMatchingConstructor
Error message
SR.ObjectReaderNoMatchingConstructor
What it means
XamlObjectReader throws this when a value's type has constructor-argument properties (x:Arguments recorded) but none of the available constructors accept arguments matching what was recorded, and the type also lacks a default-constructor fallback path. It means XAML cannot reconstruct the object with any constructor.
Solutions
- Ensure the type exposes a constructor whose parameter list matches the recorded x:Arguments in count and type
- Reorder or re-type x:Arguments to match an actual constructor
- Add an explicit constructor accepting the argument types used by the graph
- Rebuild the object graph after assembly signature changes
Example fix
// before: x:Arguments supplies <s:String>, <s:Int32> but ctor is Config(int, string) // after: swap argument order <s:Arguments><s:Int32>1</s:Int32><s:String>x</s:String></s:Arguments>
Defensive patterns
Strategy: validation
Validate before calling
bool MatchesSomeCtor(Type t, object[] args) => t.GetConstructors().Any(c => { var ps = c.GetParameters(); return ps.Length == args.Length && ps.Zip(args, (p, a) => p.ParameterType.IsInstanceOfType(a ?? (object)"")).All(x => x); }); Try / catch
try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains("NoMatchingConstructor")) { /* realign x:Arguments */ } Prevention
- Keep x:Arguments order and types aligned with a real constructor
- Bump/refresh serialized graphs when constructor signatures change
- Add a default constructor as a safe fallback
When it happens
Trigger: new XamlObjectReader(obj) where a reference type has ctor-argument members but no constructor whose parameters match the recorded argument set (count or types mismatch).
Common situations: Constructor signatures changed between assembly versions while object graphs contain instances built with the old signature; x:Arguments supplied in the wrong order or with incompatible types; custom markup extensions emitting wrong-typed arguments.
Related errors
- SR.ObjectReaderNoDefaultConstructor
- ArgumentNullException(nameof(arrayType))
- ArgumentNullException(nameof(assemblyNames))
- ArgumentNullException(nameof(contentType))
- ArgumentNullException(nameof(loaderType))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/bb9b01248e7eef0a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs:1704
{
if (!valueXamlType.IsCollection && !valueXamlType.IsDictionary) // always need to serialize items of a collection or dictionary
{
isComplete = true;
}
}
break;
}
}
if (member is null && !valueXamlType.UnderlyingType.IsValueType)
{
if (ctorArgProps.Count == 0)
{
throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderNoDefaultConstructor, value.GetType()));
}
throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderNoMatchingConstructor, value.GetType()));
}
}
private static void CheckTypeCanRoundtrip(ObjectMarkupInfo objInfo)
{
var xamlType = objInfo.XamlNode.XamlType;
if (!xamlType.IsConstructible)
{
foreach (var property in objInfo.Properties)
{
if (((MemberMarkupInfo)property).IsFactoryMethod && !xamlType.UnderlyingType.IsNested)
{
// this is the case when the class has no public constructor we can use but contains a factory method
// and the class is not nested
return;
}
}View on GitHub (pinned to 81131a70a4)