dotnet/wpf · error · XamlObjectReaderException

SR.ObjectReaderNoDefaultConstructor

Error message

SR.ObjectReaderNoDefaultConstructor

What it means

XamlObjectReader throws this when it cannot determine which member/constructor produced a value being round-tripped: the value's type is a reference type with no default constructor and no constructor-argument properties (x:Arguments) are present. Without a default constructor XAML cannot instantiate the type, so the read fails.

Solutions

  1. Add a public parameterless constructor to the type
  2. Provide x:Arguments (constructor arguments) so XamlObjectReader can pick a matching constructor (otherwise it throws ObjectReaderNoMatchingConstructor)
  3. Apply [XamlDeferLoad] or use a factory/deferring writer if the type cannot be changed
  4. Convert the value to a type with a default constructor before reading

Example fix

// before
public class Config { public Config(string name) { Name = name; } }
// after
public class Config { public Config() { } public Config(string name) { Name = name; } }
Defensive patterns

Strategy: validation

Validate before calling

bool CanRoundtrip(Type t) => !t.IsValueType && t.GetConstructor(Type.EmptyTypes) != null;
if (!CanRoundtrip(value.GetType())) throw new InvalidOperationException($"{value.GetType()} needs a parameterless ctor");

Type guard

bool HasDefaultCtor(Type t) => t.GetConstructor(Type.EmptyTypes) != null;

Try / catch

try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains("NoDefaultConstructor")) { /* supply x:Arguments or fix type */ }

Prevention

When it happens

Trigger: new XamlObjectReader(obj) on a graph containing a reference-type instance whose class has no parameterless constructor and no x:ConstructorArguments/x:Arguments were recorded.

Common situations: Classes written with only dependency-injection constructors; POCOs defined with required-arg constructors; types from third-party libraries that mandate constructor parameters; serialization frameworks migrating toward XAML.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs:1701

                        // properties.
                        //
                        if (constructorArguments.Count == properties.Count)
                        {
                            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

View on GitHub (pinned to 81131a70a4)