{"record":{"id":"aa1ed8bdf52e5cd6","repo":"dotnet/wpf","slug":"sr-xamlxmlwriterwritenotsupportedincurrentstate","errorCode":null,"errorMessage":"SR.XamlXmlWriterWriteNotSupportedInCurrentState (WriteGetObject)","messagePattern":"SR\\.XamlXmlWriterWriteNotSupportedInCurrentState \\(WriteGetObject\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":2065,"sourceCode":"                {\n                    writer.ppStateInfo.Writer.WriteStartObject(type);\n                    ThrowIfFailed(writer.ppStateInfo.Writer.Failed, \"WriteStartObject\");\n\n                    XamlNode node = new XamlNode(XamlNodeType.StartObject, type);\n                    if (writer.ppStateInfo.CurrentDepth == 0)\n                    {\n                        writer.ppStateInfo.NodesList.Add(new List<XamlNode> { node });\n                    }\n                    else\n                    {\n                        writer.ppStateInfo.NodesList[writer.ppStateInfo.NodesList.Count - 1].Add(node);\n                    }\n\n                    writer.ppStateInfo.CurrentDepth++;\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteGetObject\"));\n                }\n            }\n\n            public override void WriteEndObject(XamlXmlWriter writer)\n            {\n                writer.ppStateInfo.Writer.WriteEndObject();\n                ThrowIfFailed(writer.ppStateInfo.Writer.Failed, \"WriteEndObject\");\n\n                XamlNode node = new XamlNode(XamlNodeType.EndObject);\n\n                Debug.Assert(writer.ppStateInfo.CurrentDepth != 0);\n                Debug.Assert(writer.ppStateInfo.NodesList.Count != 0);\n                writer.ppStateInfo.NodesList[writer.ppStateInfo.NodesList.Count - 1].Add(node);\n                writer.ppStateInfo.CurrentDepth--;\n            }\n\n            public override void WriteStartMember(XamlXmlWriter writer, XamlMember property)\n            {","sourceCodeStart":2047,"sourceCodeEnd":2083,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L2047-L2083","documentation":"XamlXmlWriter.WriteGetObject throws InvalidOperationException naming \"WriteGetObject\" when the writer's current state does not allow starting a collection/dictionary get-object, i.e. the current member position cannot implicitly obtain an object value.","triggerScenarios":"Calling writer.WriteGetObject when not positioned after a member that represents a gettable collection/dictionary property, or when depth/state tracking shows the operation is invalid at this point.","commonSituations":"Serializing XAML where a collection member is written without the parent object/member structure being opened; custom writers emitting WriteGetObject for non-collection members.","solutions":["Only call WriteGetObject after WriteStartMember for a property whose getter returns a collection/dictionary.","Ensure WriteStartObject was called for the parent before WriteGetObject.","Use WriteGetObject only via a XamlReader-driven loop mirroring the source node stream."],"exampleFix":"// before: WriteGetObject with no member open\nwriter.WriteGetObject();\n// after\nwriter.WriteStartObject(parentType);\nwriter.WriteStartMember(itemsProperty);\nwriter.WriteGetObject();","handlingStrategy":"try-catch","validationCode":"bool canGet = lastNode == XamlNodeType.StartMember && currentMember.Type is { } mt &&\n    (mt.IsCollection || mt.IsDictionary);","typeGuard":null,"tryCatchPattern":"try { writer.WriteGetObject(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"WriteGetObject\")) { /* ensure member is a collection */ }","preventionTips":["Verify the current member's XamlType is a collection/dictionary before WriteGetObject.","Only emit WriteGetObject for implicit collection members (property-element syntax).","Mirror the exact node sequence from XamlXmlReader when testing."],"tags":["xaml","invalid-state","collections"],"backgroundTag":"invalid-state-transition","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"}