dotnet/wpf · error · InvalidOperationException
SR.XamlXmlWriterWriteNotSupportedInCurrentState…
Error message
SR.XamlXmlWriterWriteNotSupportedInCurrentState (WriteGetObject)
What it means
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.
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.
Example fix
// before: WriteGetObject with no member open writer.WriteGetObject(); // after writer.WriteStartObject(parentType); writer.WriteStartMember(itemsProperty); writer.WriteGetObject();
Defensive patterns
Strategy: try-catch
Validate before calling
bool canGet = lastNode == XamlNodeType.StartMember && currentMember.Type is { } mt &&
(mt.IsCollection || mt.IsDictionary); Try / catch
try { writer.WriteGetObject(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("WriteGetObject")) { /* ensure member is a collection */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Serializing XAML where a collection member is written without the parent object/member structure being opened; custom writers emitting WriteGetObject for non-collection members.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- IAmbientProvider
- InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNot…
- InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNot…
- InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty)
- InvalidOperationException(SR.XamlTypeNameNamespaceIsNull)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/aa1ed8bdf52e5cd6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:2065
{
writer.ppStateInfo.Writer.WriteStartObject(type);
ThrowIfFailed(writer.ppStateInfo.Writer.Failed, "WriteStartObject");
XamlNode node = new XamlNode(XamlNodeType.StartObject, type);
if (writer.ppStateInfo.CurrentDepth == 0)
{
writer.ppStateInfo.NodesList.Add(new List<XamlNode> { node });
}
else
{
writer.ppStateInfo.NodesList[writer.ppStateInfo.NodesList.Count - 1].Add(node);
}
writer.ppStateInfo.CurrentDepth++;
}
else
{
throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteGetObject"));
}
}
public override void WriteEndObject(XamlXmlWriter writer)
{
writer.ppStateInfo.Writer.WriteEndObject();
ThrowIfFailed(writer.ppStateInfo.Writer.Failed, "WriteEndObject");
XamlNode node = new XamlNode(XamlNodeType.EndObject);
Debug.Assert(writer.ppStateInfo.CurrentDepth != 0);
Debug.Assert(writer.ppStateInfo.NodesList.Count != 0);
writer.ppStateInfo.NodesList[writer.ppStateInfo.NodesList.Count - 1].Add(node);
writer.ppStateInfo.CurrentDepth--;
}
public override void WriteStartMember(XamlXmlWriter writer, XamlMember property)
{View on GitHub (pinned to 81131a70a4)