dotnet/wpf · error · InvalidOperationException

SR.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollectio…

Error message

SR.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollections

What it means

WriteObject throws InvalidOperationException when isObjectFromMember is true inside a StartMember frame whose member type is neither a collection nor a dictionary. GetObject-style member writes are only meaningful for collection/dictionary members; arrays and plain reference-type members cannot be materialized this way.

Solutions

  1. Use WriteStartObject (new object) instead of WriteObject(isObjectFromMember:true) for array or non-collection members.
  2. Change the member type to a List<T>/Dictionary supporting collection, or wrap the array in a collection type.
  3. Guard the call: only pass isObjectFromMember:true when memberType.IsCollection || memberType.IsDictionary.
  4. Fix custom XamlType definitions that misreport IsCollection/IsDictionary.

Example fix

// before
writer.StartMember(arrayMember);
writer.WriteObject(arrayType, isObjectFromMember: true); // throws
// after
writer.StartMember(arrayMember);
writer.WriteStartObject(arrayType);
Defensive patterns

Strategy: type-guard

Validate before calling

bool ok = memberType is null || memberType.IsCollection || memberType.IsDictionary;

Type guard

bool CanGetObject(XamlType t) => t is not null && (t.IsCollection || t.IsDictionary) && !t.IsArray;

Try / catch

try { writer.WriteObject(type, true); } catch (InvalidOperationException ex) when (ex.Message.Contains("isObjectFromMember")) { writer.WriteStartObject(type); }

Prevention

When it happens

Trigger: Calling WriteObject(type, isObjectFromMember: true) while frame.AllocatingNodeType == StartMember and frame.Member.Type is non-null but IsCollection == false and IsDictionary == false — e.g. member typed as an array or a scalar/reference property.

Common situations: Serializers that call WriteObject on array-typed properties expecting implicit collection behavior, porting XamlWriter code that assumed all member objects support GetObject, mislabeled member types in custom XamlSchemaContext.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7eeaeaa1a18cf1ac. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:1399

                writer.isFirstElementOfWhitespaceSignificantCollection = false;

                if (isObjectFromMember)
                {
                    if (!writer.namespaceScopes.Peek().IsEmpty())
                    {
                        throw new InvalidOperationException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState);
                    }

                    Frame tempFrame = writer.namespaceScopes.Pop();
                    Frame frame = writer.namespaceScopes.Peek();
                    writer.namespaceScopes.Push(tempFrame);

                    if (frame.AllocatingNodeType == XamlNodeType.StartMember)
                    {
                        XamlType memberType = frame.Member.Type;
                        if (memberType is not null && !memberType.IsCollection && !memberType.IsDictionary)
                        {
                            throw new InvalidOperationException(SR.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollections);
                        }
                    }

                    writer.currentState = InRecord.State;
                }
                else
                {
                    WriteStartElementForObject(writer, type);
                    writer.currentState = InRecordTryAttributes.State;
                 }
            }
        }

        // Follows InMember after an Atom and prevents writing two atoms
        // in a row
        //
        private class InMemberAfterValue : WriterState
        {

View on GitHub (pinned to 81131a70a4)