{"record":{"id":"deded941f454e84e","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeoftype-serializableobjectcontext-deded9","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/NGCUIElementCollectionSerializerAsync.cs","lineNumber":127,"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            SerializeUIElements(serializableObjectContext);\n        }\n\n        #endregion Internal Methods\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        private","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCUIElementCollectionSerializerAsync.cs#L109-L145","documentation":"XpsSerializationException thrown by NGCUIElementCollectionSerializerAsync.PersistObjectData when the serialization context's TargetObject cannot be cast to IEnumerable. This serializer is only designed to serialize collections (specifically collections of page/UI elements) during XPS document writing, so it rejects any object it was handed that is not enumerable.","triggerScenarios":"Calling XpsDocument/XpsSerializationManager APIs with a SerializableObjectContext whose TargetObject is a non-IEnumerable object (e.g. a single UIElement or arbitrary object) instead of a collection such as UIElementCollection or a FixedDocument/PageContent collection.","commonSituations":"Passing the wrong object to a custom serializer or document sequence during XPS export; registering this serializer for a type it was not designed for; refactoring printing code so a single element instead of its parent collection reaches the serializer.","solutions":["Ensure the object passed to the serializer's PersistObjectData has a TargetObject that implements System.Collections.IEnumerable (e.g. a UIElementCollection).","If serializing a single element, wrap it in a collection or route it to the appropriate single-object serializer instead of NGCUIElementCollectionSerializerAsync.","Verify custom serializer registration/mapping does not bind this collection serializer to non-collection types.","Debug serializableObjectContext.TargetObject.GetType() at the throw site to identify which non-enumerable type leaked in."],"exampleFix":"// before\nserializerContext.TargetObject = mySingleVisual; // not IEnumerable\n// after\nserializerContext.TargetObject = myVisualCollection; // implements IEnumerable","handlingStrategy":"validation","validationCode":"if (context?.TargetObject is not System.Collections.IEnumerable)\n    throw new ArgumentException(\"TargetObject must implement IEnumerable\", nameof(context));","typeGuard":"bool IsEnumerableTarget(SerializableObjectContext c) => c?.TargetObject is System.Collections.IEnumerable;","tryCatchPattern":"try { serializer.PersistObjectData(context, ...); }\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"IEnumerable\"))\n{ /* log type of context.TargetObject and fall back to single-object serializer */ }","preventionTips":["Always hand collection serializers an object implementing IEnumerable.","Unit-test serializer registration mappings against the types they will receive.","Log TargetObject.GetType() before serialization in debug builds.","Wrap single elements in a containing collection when a collection serializer is required."],"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-22T01:17:13.364Z"}