{"record":{"id":"71ed79ffec6cba4f","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeoftype-serializableobjectcontext-71ed79","errorCode":null,"errorMessage":"SR.Format(SR.MustBeOfType, \"serializableObjectContext.TargetObject\", typeof(System.Collections.Generic.IEnumerable<DocumentReference>))","messagePattern":"SR\\.Format\\(SR\\.MustBeOfType, \"serializableObjectContext\\.TargetObject\", typeof\\(System\\.Collections\\.Generic\\.IEnumerable<DocumentReference>\\)\\)","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentReferenceCollectionSerializer.cs","lineNumber":45,"sourceCode":"\n        /// <summary>\n        /// \n        /// </summary>\n        internal\n        override\n        void\n        PersistObjectData(\n            SerializableObjectContext   serializableObjectContext\n            )\n        {\n            ArgumentNullException.ThrowIfNull(serializableObjectContext);\n\n            // get DocumentReferenceCollection\n            System.Collections.Generic.IEnumerable<DocumentReference> enumerableObject = serializableObjectContext.TargetObject as System.Collections.Generic.IEnumerable<DocumentReference>;\n\n            if (enumerableObject == null)\n            {\n                throw new XpsSerializationException(SR.Format(SR.MustBeOfType, \"serializableObjectContext.TargetObject\", typeof(System.Collections.Generic.IEnumerable<DocumentReference>)));\n            }\n\n            SerializeDocumentReferences(serializableObjectContext);\n        }\n\n        /// <summary>\n        /// This is being called to serialize the DocumentReference items\n        /// contained within the colleciton\n        /// </summary>\n        private\n        void\n        SerializeDocumentReferences(\n            SerializableObjectContext   serializableObjectContext\n            )\n        {\n            //\n            // Serialize each DocumentReference in DocumentReferenceColleciton\n            //","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentReferenceCollectionSerializer.cs#L27-L63","documentation":"ReachDocumentReferenceCollectionSerializer.PersistObjectData throws this XpsSerializationException when the serializer's target object is not an IEnumerable<DocumentReference>. The serializer is registered for DocumentReference collections; if the object context handed to it cannot be cast (the 'as' returns null), the input type doesn't match the serializer's contract, so serialization aborts with a MustBeOfType message naming the expected type.","triggerScenarios":"Registering or invoking ReachDocumentReferenceCollectionSerializer for an object whose TargetObject is not IEnumerable<DocumentReference> — e.g., a single DocumentReference, a FixedDocumentSequence, or a custom collection of a different element type passed through XpsSerializationManager.","commonSituations":"Custom serializer registration tables mapping the wrong type to this serializer; passing a DocumentSequence or package-level object to the collection serializer by mistake; refactors that changed the collection element type so the 'as' cast fails at runtime.","solutions":["Ensure the object serialized at this point is (or implements) IEnumerable<DocumentReference>; inspect what type is being passed to SerializeObject.","Fix the serializer registration/type mapping so ReachDocumentReferenceCollectionSerializer is only used for DocumentReference collections.","Cast or wrap the data into IEnumerable<DocumentReference> before handing it to the serialization manager."],"exampleFix":"// before: wrong type handed to serializer\nserializer.SerializeObject(fixedDocumentSequence);\n\n// after: serialize the DocumentReferences collection\nIEnumerable<DocumentReference> refs = fixedDocumentSequence.References;\nserializer.SerializeObject(refs);","handlingStrategy":"type-guard","validationCode":"if (target is not IEnumerable<DocumentReference>)\n    throw new ArgumentException(nameof(target), \"Serializer expects IEnumerable<DocumentReference>\");","typeGuard":"bool IsDocRefCollection(object o) => o is IEnumerable<DocumentReference>;","tryCatchPattern":"try { serializer.SerializeObject(obj); }\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"MustBeOfType\")) {\n    // wrong serializer for this object type; fix registration\n}","preventionTips":["Register serializers only for their documented contract types","Unit-test serializer type dispatch with representative objects","Validate target object type before serialization"],"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-21T21:30:21.729Z"}