{"record":{"id":"0392385d5b2ba41b","repo":"dotnet/wpf","slug":"sr-mustbeoftype-reachuielementcollectionserializer","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/ReachUIElementCollectionSerializer.cs","lineNumber":88,"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":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachUIElementCollectionSerializer.cs#L70-L106","documentation":"ReachUIElementCollectionSerializer.PersistObjectData only knows how to serialize objects implementing IEnumerable, because it enumerates the target to serialize each contained UI element. If the context's TargetObject is not enumerable, the serializer was matched to the wrong object type and an XpsSerializationException naming IEnumerable is thrown.","triggerScenarios":"Serializing an object whose type resolved to ReachUIElementCollectionSerializer (e.g. a UIElementCollection-like container) but whose runtime TargetObject does not implement IEnumerable — typically a custom collection or container type that mimics the serialized shape without implementing IEnumerable.","commonSituations":"Custom panels/collections substituted for standard WPF collections in a serialized visual tree; type-substitution bugs where a serializer is selected for a non-enumerable object.","solutions":["Make the container implement IEnumerable (and ideally ICollection/UIElementCollection semantics) so it is serializable","Use standard WPF collections (UIElementCollection, VisualCollection) in serialized trees","Ensure the serializer selection maps the object to the correct serializer type rather than the collection serializer"],"exampleFix":"// before\nclass MyContainer : Visual { public List<Visual> Items = new List<Visual>(); }\n// after\nclass MyContainer : Visual, IEnumerable { public List<Visual> Items = new List<Visual>(); public IEnumerator GetEnumerator() => Items.GetEnumerator(); }","handlingStrategy":"type-guard","validationCode":"if (obj is not IEnumerable) throw new ArgumentException(\"Target must implement IEnumerable to use the UI element collection serializer\");","typeGuard":"bool IsEnumerableCollection(object o) => o is IEnumerable;","tryCatchPattern":"try { serializer.SerializeObject(collection); }\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"IEnumerable\")) { /* use a different serializer or fix the container type */ }","preventionTips":["Only register the collection serializer for IEnumerable targets","Implement IEnumerable on custom containers","Prefer standard WPF collections in serialized trees"],"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"}