{"record":{"id":"efb928b7e927749e","repo":"dotnet/wpf","slug":"sr-mustbeoftype-reachuielementcollectionserializerasync","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/ReachUIElementCollectionSerializerAsync.cs","lineNumber":128,"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":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachUIElementCollectionSerializerAsync.cs#L110-L146","documentation":"The async variant ReachUIElementCollectionSerializerAsync.PersistObjectData has the same requirement as the sync version: the context's TargetObject must implement IEnumerable so each contained element can be serialized asynchronously. A non-enumerable target indicates a serializer/target mismatch and triggers an XpsSerializationException naming IEnumerable.","triggerScenarios":"Asynchronously serializing an object routed to ReachUIElementCollectionSerializerAsync whose TargetObject does not implement IEnumerable — e.g. a custom non-enumerable container in the visual tree during an XpsDocument.BeginWrite operation.","commonSituations":"Custom collections/panels in trees saved asynchronously to XPS; mismatched serializer selection for the target type in async pipelines.","solutions":["Make the container type implement IEnumerable","Use standard WPF collections so the built-in serializer matches","Fix serializer registration so non-collection objects get the appropriate serializer"],"exampleFix":"// before\nclass MyPanel : Panel { } // stores children in a private non-enumerable custom store\n// after\nclass MyPanel : Panel, IEnumerable { public override UIElementCollection Children { get; } public IEnumerator GetEnumerator() => Children.GetEnumerator(); }","handlingStrategy":"type-guard","validationCode":"if (obj is not IEnumerable) throw new ArgumentException(\"Async collection serialization requires an IEnumerable target\");","typeGuard":"bool IsEnumerableCollection(object o) => o is IEnumerable;","tryCatchPattern":"try { asyncSerializer.SerializeObject(collection); }\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"IEnumerable\")) { /* fix container type or serializer mapping */ }","preventionTips":["Implement IEnumerable on custom containers","Keep serializer registrations in sync between sync and async pipelines","Use standard WPF collections"],"tags":["wpf","xps","serialization","async","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"}