{"record":{"id":"7eeaeaa1a18cf1ac","repo":"dotnet/wpf","slug":"sr-xamlxmlwriter","errorCode":null,"errorMessage":"SR.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollections","messagePattern":"SR\\.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollections","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1399,"sourceCode":"                writer.isFirstElementOfWhitespaceSignificantCollection = false;\n\n                if (isObjectFromMember)\n                {\n                    if (!writer.namespaceScopes.Peek().IsEmpty())\n                    {\n                        throw new InvalidOperationException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState);\n                    }\n\n                    Frame tempFrame = writer.namespaceScopes.Pop();\n                    Frame frame = writer.namespaceScopes.Peek();\n                    writer.namespaceScopes.Push(tempFrame);\n\n                    if (frame.AllocatingNodeType == XamlNodeType.StartMember)\n                    {\n                        XamlType memberType = frame.Member.Type;\n                        if (memberType is not null && !memberType.IsCollection && !memberType.IsDictionary)\n                        {\n                            throw new InvalidOperationException(SR.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollections);\n                        }\n                    }\n\n                    writer.currentState = InRecord.State;\n                }\n                else\n                {\n                    WriteStartElementForObject(writer, type);\n                    writer.currentState = InRecordTryAttributes.State;\n                 }\n            }\n        }\n\n        // Follows InMember after an Atom and prevents writing two atoms\n        // in a row\n        //\n        private class InMemberAfterValue : WriterState\n        {","sourceCodeStart":1381,"sourceCodeEnd":1417,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1381-L1417","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use WriteStartObject (new object) instead of WriteObject(isObjectFromMember:true) for array or non-collection members.","Change the member type to a List<T>/Dictionary supporting collection, or wrap the array in a collection type.","Guard the call: only pass isObjectFromMember:true when memberType.IsCollection || memberType.IsDictionary.","Fix custom XamlType definitions that misreport IsCollection/IsDictionary."],"exampleFix":"// before\nwriter.StartMember(arrayMember);\nwriter.WriteObject(arrayType, isObjectFromMember: true); // throws\n// after\nwriter.StartMember(arrayMember);\nwriter.WriteStartObject(arrayType);","handlingStrategy":"type-guard","validationCode":"bool ok = memberType is null || memberType.IsCollection || memberType.IsDictionary;","typeGuard":"bool CanGetObject(XamlType t) => t is not null && (t.IsCollection || t.IsDictionary) && !t.IsArray;","tryCatchPattern":"try { writer.WriteObject(type, true); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"isObjectFromMember\")) { writer.WriteStartObject(type); }","preventionTips":["Only pass isObjectFromMember:true for collection/dictionary members.","Check member.Type.IsCollection/IsDictionary first.","Treat arrays as StartObject writes."],"tags":["xaml","type-mismatch","collection"],"backgroundTag":"type-mismatch","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"}