{"record":{"id":"5892fc7612b4ae58","repo":"dotnet/wpf","slug":"sr-mustbeoftype-reachpagecontentcollectionserializer","errorCode":null,"errorMessage":"SR.MustBeOfType","messagePattern":"SR\\.MustBeOfType","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachPageContentCollectionSerializer.cs","lineNumber":65,"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        #endregion Internal Methods\n\n        #region Private Methods\n\n        /// <summary>\n        /// This is being called to serialize the Page Content items\n        /// contained within the collection\n        /// </summary>\n        /// <param name=\"serializableObjectContext\">\n        /// The context of the object to be serialized at this time.","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachPageContentCollectionSerializer.cs#L47-L83","documentation":"ReachPageContentCollectionSerializer.PersistObjectData throws this XpsSerializationException when the SerializableObjectContext's TargetObject does not implement IEnumerable. This serializer only knows how to walk a collection of PageContent items, so the target being serialized must be an enumerable collection; anything else is a programming error in the serialization tree.","triggerScenarios":"Calling SerializeObject/PersistObjectData with a ReachSerializerContext whose TargetObject is not an IEnumerable — e.g. passing a single PageContent, a FixedPage, or a non-collection object to the collection serializer instead of a PageContentCollection (such as a FixedPageContent sequence from a FixedDocument).","commonSituations":"Wiring up a custom XPS serialization pipeline and registering the collection serializer for the wrong object type; serializing a FixedDocument but handing the context the document rather than its Pages collection; type changes after refactoring where the target stopped implementing IEnumerable.","solutions":["Ensure the object passed to the collection serializer's SerializeObject implements IEnumerable and enumerates PageContent items","If serializing a single page, use ReachPageContentSerializer instead of the collection serializer","Check how the SerializableObjectContext was built — the TargetObject should come from iterating the parent document's Pages property","Verify custom serializer registration so each object type maps to the correct serializer"],"exampleFix":"// before\nserializer.SerializeObject(fixedPage); // wrong serializer: collection serializer gets non-IEnumerable target\n// after\nvar pageContentCollection = fixedDocument.Pages; // IEnumerable<PageContent>\nserializer.SerializeObject(pageContentCollection);","handlingStrategy":"validation","validationCode":"if (obj is not IEnumerable) throw new ArgumentException(\"Target must be an IEnumerable of PageContent\");","typeGuard":"bool IsEnumerableCollection(object o) => o is IEnumerable;","tryCatchPattern":null,"preventionTips":["Assert TargetObject is IEnumerable before constructing SerializableObjectContext","Route collections to the collection serializer and single pages to the page serializer","Write unit tests covering the document-tree walk types"],"tags":["wpf","xps-serialization","type-mismatch"],"backgroundTag":"incompatible-source-type","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}