{"record":{"id":"bb94f205228dc05f","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeoftype-serializableobjectcontext-bb94f2","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/ReachDocumentReferenceCollectionSerializerAsync.cs","lineNumber":83,"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        internal\n        override\n        void\n        EndPersistObjectData(\n            )\n        {\n            //\n            // do nothing in this stage\n            //\n        }\n\n        /// <summary>\n        /// This is being called to serialize the DocumentReference items","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentReferenceCollectionSerializerAsync.cs#L65-L101","documentation":"The asynchronous variant (ReachDocumentReferenceCollectionSerializerAsync.PersistObjectData) throws the same XpsSerializationException as its synchronous twin when the context's TargetObject is not an IEnumerable<DocumentReference>. The async serializer's contract requires a DocumentReference collection; any other target object fails the 'as' cast and aborts with the MustBeOfType message.","triggerScenarios":"Async XPS serialization (XpsSerializationManagerAsync) invoked on an object whose TargetObject is not IEnumerable<DocumentReference> — e.g., a single FixedDocument, FixedDocumentSequence, or mistyped collection — routed to this serializer by the registration table.","commonSituations":"Using async serialization of an XPS document with the wrong root object; custom serializer registration mapping DocumentReference collections to the wrong type; code paths shared with sync serialization where the object type differs.","solutions":["Pass an IEnumerable<DocumentReference> (e.g., FixedDocumentSequence.References) as the serialization target for the async collection serializer.","Correct the serializer registration so the async collection serializer is only selected for DocumentReference collections.","Add an upfront type check on the target object before starting async serialization to fail fast with a clearer error."],"exampleFix":"// before\nasyncSerializer.SerializeObject(singleDocument);\n\n// after\nif (target is IEnumerable<DocumentReference> refs)\n    asyncSerializer.SerializeObject(refs);","handlingStrategy":"type-guard","validationCode":"if (serializableObjectContext.TargetObject is not IEnumerable<DocumentReference>)\n    throw new ArgumentException(\"Async serializer expects IEnumerable<DocumentReference>\");","typeGuard":"bool IsAsyncDocRefCollection(SerializableObjectContext ctx) => ctx.TargetObject is IEnumerable<DocumentReference>;","tryCatchPattern":"try { await asyncSerializeAsync(obj); }\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"MustBeOfType\")) {\n    // target type mismatch in async path\n}","preventionTips":["Pass FixedDocumentSequence.References to async collection serialization","Mirror sync/async target types in shared code paths","Type-check before starting async serialization"],"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"}