{"record":{"id":"4bf64ed0ad0ad969","repo":"dotnet/wpf","slug":"sr-objectreadernodefaultconstructor","errorCode":null,"errorMessage":"SR.ObjectReaderNoDefaultConstructor","messagePattern":"SR\\.ObjectReaderNoDefaultConstructor","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs","lineNumber":1701,"sourceCode":"                        // properties.\n                        //\n                        if (constructorArguments.Count == properties.Count)\n                        {\n                            if (!valueXamlType.IsCollection && !valueXamlType.IsDictionary) // always need to serialize items of a collection or dictionary\n                            {\n                                isComplete = true;\n                            }\n                        }\n\n                        break;\n                    }\n                }\n\n                if (member is null && !valueXamlType.UnderlyingType.IsValueType)\n                {\n                    if (ctorArgProps.Count == 0)\n                    {\n                        throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderNoDefaultConstructor, value.GetType()));\n                    }\n\n                    throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderNoMatchingConstructor, value.GetType()));\n                }\n            }\n\n            private static void CheckTypeCanRoundtrip(ObjectMarkupInfo objInfo)\n            {\n                var xamlType = objInfo.XamlNode.XamlType;\n                if (!xamlType.IsConstructible)\n                {\n                    foreach (var property in objInfo.Properties)\n                    {\n                        if (((MemberMarkupInfo)property).IsFactoryMethod && !xamlType.UnderlyingType.IsNested)\n                        {\n                            // this is the case when the class has no public constructor we can use but contains a factory method\n                            // and the class is not nested\n","sourceCodeStart":1683,"sourceCodeEnd":1719,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs#L1683-L1719","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a public parameterless constructor to the type","Provide x:Arguments (constructor arguments) so XamlObjectReader can pick a matching constructor (otherwise it throws ObjectReaderNoMatchingConstructor)","Apply [XamlDeferLoad] or use a factory/deferring writer if the type cannot be changed","Convert the value to a type with a default constructor before reading"],"exampleFix":"// before\npublic class Config { public Config(string name) { Name = name; } }\n// after\npublic class Config { public Config() { } public Config(string name) { Name = name; } }","handlingStrategy":"validation","validationCode":"bool CanRoundtrip(Type t) => !t.IsValueType && t.GetConstructor(Type.EmptyTypes) != null;\nif (!CanRoundtrip(value.GetType())) throw new InvalidOperationException($\"{value.GetType()} needs a parameterless ctor\");","typeGuard":"bool HasDefaultCtor(Type t) => t.GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains(\"NoDefaultConstructor\")) { /* supply x:Arguments or fix type */ }","preventionTips":["Design serializable POCOs with public parameterless constructors","Provide x:Arguments when only parameterized ctors exist","Avoid exposing DI-only types in XAML-round-tripped graphs"],"tags":["xaml","constructor","serialization"],"backgroundTag":"missing-default-constructor","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"}