{"record":{"id":"47ccc1dbb7e9312c","repo":"dotnet/wpf","slug":"serializableobjectcontext-targetobject-must-be-of-type","errorCode":null,"errorMessage":"'serializableObjectContext.TargetObject' must be of type 'System.Collections.IEnumerable'.","messagePattern":"'serializableObjectContext\\.TargetObject' must be of type 'System\\.Collections\\.IEnumerable'\\.","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCPageContentCollectionSerializerAsync.cs","lineNumber":113,"sourceCode":"        /// point of the serialization process.\n        /// </summary>\n        /// <param name=\"serializableObjectContext\">\n        /// The context of the object to be serialized at this time.\n        /// </param>\n        internal\n        override\n        void\n        PersistObjectData(\n            SerializableObjectContext   serializableObjectContext\n            )\n        {\n            ArgumentNullException.ThrowIfNull(serializableObjectContext);\n\n            IEnumerable enumerableObject = serializableObjectContext.TargetObject as IEnumerable;\n\n            if (enumerableObject == null)\n            {\n                throw new XpsSerializationException(SR.Format(SR.MustBeOfType, \"serializableObjectContext.TargetObject\", typeof(IEnumerable)));\n            }\n\n            //\n            // Serialize the PageContent Items contained within the collection \n            //\n            SerializePageContents(serializableObjectContext);\n        }\n\n        internal\n        override\n        void\n        EndPersistObjectData(\n            )\n        {\n            //\n            // do nothing in this stage\n            //\n        }","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCPageContentCollectionSerializerAsync.cs#L95-L131","documentation":"NGCPageContentCollectionSerializerAsync.PersistObjectData expects the serializableObjectContext's TargetObject to implement IEnumerable (a collection of PageContent). If it does not, it throws XpsSerializationException stating the target must be of type IEnumerable.","triggerScenarios":"Asynchronously serializing a context whose TargetObject is a single PageContent or another non-collection object instead of a PageContent collection (e.g. FixedPageContent/PageContentCollection).","commonSituations":"Passing the wrong object to the collection serializer — usually a wiring bug in custom serialization managers or when calling SaveAsXaml with an object that gets routed to the collection serializer.","solutions":["Pass a PageContentCollection (or any IEnumerable of PageContent) to this serializer","Use NGCPageContentSerializerAsync for a single PageContent","Verify the object graph being serialized (e.g. iterate FixedDocument.Pages rather than a lone page)"],"exampleFix":"// before\nmanager.SaveAsXaml(pageContent); // routed to collection serializer\n// after\nmanager.SaveAsXaml(fixedDocument.Pages); // IEnumerable of PageContent","handlingStrategy":"validation","validationCode":"if (obj is not IEnumerable)\n    throw new ArgumentException(\"Target must be a PageContent collection (IEnumerable)\");\nmanager.SaveAsXaml(obj);","typeGuard":"bool IsPageContentCollection(object o) => o is IEnumerable;","tryCatchPattern":"try\n{\n    manager.SaveAsXaml(obj);\n}\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"IEnumerable\"))\n{\n    // route single PageContent to the page content serializer instead\n}","preventionTips":["Match the serializer to the object kind: collections to collection serializer, single pages to page serializer","Verify the object graph (FixedDocument.Pages) before serializing"],"tags":["wpf","xps","serialization","type-mismatch"],"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"}