{"record":{"id":"80b2d31e1e006a53","repo":"dotnet/wpf","slug":"sr-objectreadertypeisnested","errorCode":null,"errorMessage":"SR.ObjectReaderTypeIsNested","messagePattern":"SR\\.ObjectReaderTypeIsNested","errorType":"exception","errorClass":"XamlObjectReaderException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs","lineNumber":1726,"sourceCode":"            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\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)","sourceCodeStart":1708,"sourceCodeEnd":1744,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs#L1708-L1744","documentation":"XamlObjectReader throws this during round-trip validation when the type of an object in the graph is a nested (compiler-generated or manually nested) type. XAML cannot declare a nested type via markup (no xml-namespaced reference form), so it refuses to read the graph to avoid producing invalid XAML.","triggerScenarios":"new XamlObjectReader(obj) where the graph contains an instance whose System.Xaml XamlType.UnderlyingType.IsNested is true (e.g. a lambda/closure display class or a class declared inside another class) and the type cannot round-trip otherwise.","commonSituations":"Serializing graphs that captured anonymous-type or closure instances; passing objects created inside methods (compiler-generated nested classes); types intentionally nested for encapsulation; deserializing object trees built with LINQ.","solutions":["Move the type out to a top-level (non-nested) public class","Replace closure/anonymous-type instances with named top-level types before reading","Add a TypeConverter so the value can be expressed as a string/extension instead of element markup","Exclude the nested-typed property from the graph (e.g. [XamlSerializationVisibility] / ignore)"],"exampleFix":"// before\npublic class Outer { private class Payload { public int X; } }\n// after: move to its own file\npublic class Payload { public int X; }","handlingStrategy":"type-guard","validationCode":"if (type.IsNested) throw new InvalidOperationException($\"{type.FullName} is nested and cannot roundtrip via XAML\");","typeGuard":"bool IsXamlRoundtrippable(Type t) => !t.IsNested && !t.IsGenericType && (t.IsPublic || t.IsVisible);","tryCatchPattern":"try { using var r = new XamlObjectReader(obj); ... } catch (XamlObjectReaderException ex) when (ex.Message.Contains(\"TypeIsNested\")) { /* hoist type or strip property */ }","preventionTips":["Avoid anonymous types/closures in serializable object graphs","Keep XAML-serializable types top-level and public","Add TypeConverters where element markup is not feasible"],"tags":["xaml","nested-type","serialization"],"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"}