{"record":{"id":"55354a74dbb50a69","repo":"dotnet/wpf","slug":"sr-objectreadertypecannotroundtrip","errorCode":null,"errorMessage":"SR.ObjectReaderTypeCannotRoundtrip","messagePattern":"SR\\.ObjectReaderTypeCannotRoundtrip","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs","lineNumber":1730,"sourceCode":"                {\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\n                            return;\n                        }\n                    }\n\n                    if (xamlType.UnderlyingType.IsNested)\n                    {\n                        throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderTypeIsNested, xamlType.Name));\n                    }\n                    else\n                    {\n                        throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderTypeCannotRoundtrip, xamlType.Name));\n                    }\n                }\n            }\n\n            public void AssignName(SerializerContext context)\n            {\n                if (Name is null)\n                {\n                    Name = context.AllocateIdentifier();\n                    AddNameProperty(context);\n                }\n            }\n\n            public void AssignName(string name, SerializerContext context)\n            {\n                if (Name is null)\n                {\n                    Name = name;","sourceCodeStart":1712,"sourceCodeEnd":1748,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs#L1712-L1748","documentation":"XamlObjectReader throws this during round-trip validation when an object's type cannot be represented in XAML at all — the type has no usable constructor/member representation for markup (and is not a nested type, which gets a different message). The reader aborts because writing the graph as XAML would be impossible.","triggerScenarios":"new XamlObjectReader(obj) where CheckTypeCanRoundtrip determines a XamlType in the graph lacks a valid instantiation path (no default ctor, no matching ctor/arguments, no converter/extension representation).","commonSituations":"Graphs containing types designed purely for code (e.g. delegate-bearing, generic-closed types without converters, internal-only types); third-party objects not designed for markup round-tripping; types without TypeConverters used in collections.","solutions":["Give the type a public parameterless constructor and settable public properties","Add a TypeConverter (with ConvertFrom/ConvertTo) so the value round-trips via attribute syntax","Replace the value with a XAML-representable type before reading","Skip/omit the offending member from the object graph"],"exampleFix":"// before: type with only internal ctor, no converter\n// after\npublic class Point2 { public Point2() { } public double X { get; set; } public double Y { get; set; } }","handlingStrategy":"validation","validationCode":"bool Roundtrippable(Type t) => t.GetConstructor(Type.EmptyTypes) != null || t.GetConstructors().Any(c => c.GetParameters().Length > 0) && !t.IsNested && t.IsVisible;\nif (!Roundtrippable(obj.GetType())) throw new InvalidOperationException(\"Type cannot roundtrip via XAML\");","typeGuard":"bool CanRoundtrip(Type t) => !t.IsNested && t.IsVisible && (t.GetConstructor(Type.EmptyTypes) != null || t.GetConvertFrom != null); // adjust: use TypeConverter check\n// practical guard:\nbool HasConverterOrDefaultCtor(Type t) => !t.IsNested && (t.GetConstructor(Type.EmptyTypes) != null || TypeDescriptor.GetConverter(t).CanConvertFrom(typeof(string)));","tryCatchPattern":"try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains(\"TypeCannotRoundtrip\")) { /* substitute or omit value */ }","preventionTips":["Give XAML-facing types public default ctors and settable properties","Add TypeConverters for types expressible only as strings/extensions","Audit third-party values in graphs before XAML round-tripping"],"tags":["xaml","serialization","type-system"],"backgroundTag":"unsupported-operation","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"}