{"record":{"id":"94041e8177908bfe","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeoftype-serializableobjectcontext","errorCode":null,"errorMessage":"SR.Format(SR.MustBeOfType, \"serializableObjectContext.TargetObject\", typeof(IEnumerable))","messagePattern":"SR\\.Format\\(SR\\.MustBeOfType, \"serializableObjectContext\\.TargetObject\", typeof\\(IEnumerable\\)\\)","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCSerializer.cs","lineNumber":676,"sourceCode":"\n        #endregion Constructor\n\n\n        /// <summary>\n        ///\n        /// </summary>\n        public\n        override\n        void\n        SerializeObject(\n            Object serializedObject\n            )\n        {\n            IEnumerable enumerableObject =  serializedObject 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(enumerableObject);\n        }\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=\"enumerableObject\">\n        /// The context of the object to be serialized at this time.\n        /// </param>\n        private","sourceCodeStart":658,"sourceCodeEnd":694,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCSerializer.cs#L658-L694","documentation":"The PageContent collection serializer requires the serializable object context's TargetObject to implement IEnumerable; it throws XpsSerializationException(SR.Format(SR.MustBeOfType, \"serializableObjectContext.TargetObject\", typeof(IEnumerable))) otherwise. It enumerates PageContent items inside a FixedDocument, so a non-enumerable target is an invariant violation.","triggerScenarios":"Serializing a FixedDocument's PageContents where the context's TargetObject is not an IEnumerable (e.g. a single PageContent or a wrong property was pushed as the context target).","commonSituations":"Custom serializer contexts built by hand with the wrong TargetObject; internal context wiring broken after overriding serialization steps.","solutions":["Ensure the context TargetObject is the FixedDocument's Pages/PageContent collection (IEnumerable), not a single page.","Fix custom context construction so it passes the enumerable collection property.","Validate with 'is IEnumerable' before creating the context."],"exampleFix":"// before\nnew NgcSerializableObjectContext(pageContent, \"\"); // single item\n// after\nnew NgcSerializableObjectContext(fixedDocument.Pages, \"\"); // IEnumerable collection","handlingStrategy":"validation","validationCode":"if (serializableObjectContext?.TargetObject is not IEnumerable)\n    throw new InvalidOperationException(\"PageContent serializer context target must be an IEnumerable collection\");","typeGuard":"bool HasEnumerableTarget(NgcSerializableObjectContext ctx) => ctx?.TargetObject is IEnumerable;","tryCatchPattern":"try { SerializePageContents(context); }\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"IEnumerable\")) { /* rebuild the context with the collection property */ }","preventionTips":["Always build serializer contexts from collection properties (e.g. FixedDocument.Pages)","Never pass a single PageContent as a collection context target","Assert context target types in serializer unit tests"],"tags":["wpf","xps","serialization","type-mismatch","ienumerable"],"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"}