dotnet/wpf · error · XpsSerializationException
SR.Format(SR.MustBeOfType…
Error message
SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(System.Collections.Generic.IEnumerable<DocumentReference>))
What it means
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.
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.
Example fix
// before: wrong type handed to serializer serializer.SerializeObject(fixedDocumentSequence); // after: serialize the DocumentReferences collection IEnumerable<DocumentReference> refs = fixedDocumentSequence.References; serializer.SerializeObject(refs);
Defensive patterns
Strategy: type-guard
Validate before calling
if (target is not IEnumerable<DocumentReference>)
throw new ArgumentException(nameof(target), "Serializer expects IEnumerable<DocumentReference>"); Type guard
bool IsDocRefCollection(object o) => o is IEnumerable<DocumentReference>;
Try / catch
try { serializer.SerializeObject(obj); }
catch (XpsSerializationException ex) when (ex.Message.Contains("MustBeOfType")) {
// wrong serializer for this object type; fix registration
} Prevention
- Register serializers only for their documented contract types
- Unit-test serializer type dispatch with representative objects
- Validate target object type before serialization
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- 'serializableObjectContext.TargetObject' must be of type…
- SR.Format(SR.MustBeOfType…
- SR.Format(SR.MustBeOfType…
- SR.Format(SR.MustBeOfType…
- SR.MustBeOfType
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/71ed79ffec6cba4f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentReferenceCollectionSerializer.cs:45
/// <summary>
///
/// </summary>
internal
override
void
PersistObjectData(
SerializableObjectContext serializableObjectContext
)
{
ArgumentNullException.ThrowIfNull(serializableObjectContext);
// get DocumentReferenceCollection
System.Collections.Generic.IEnumerable<DocumentReference> enumerableObject = serializableObjectContext.TargetObject as System.Collections.Generic.IEnumerable<DocumentReference>;
if (enumerableObject == null)
{
throw new XpsSerializationException(SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(System.Collections.Generic.IEnumerable<DocumentReference>)));
}
SerializeDocumentReferences(serializableObjectContext);
}
/// <summary>
/// This is being called to serialize the DocumentReference items
/// contained within the colleciton
/// </summary>
private
void
SerializeDocumentReferences(
SerializableObjectContext serializableObjectContext
)
{
//
// Serialize each DocumentReference in DocumentReferenceColleciton
//View on GitHub (pinned to 81131a70a4)