dotnet/wpf · error · XpsSerializationException
SR.MustBeOfType
Error message
SR.MustBeOfType
What it means
Async variant of the same guard: ReachPageContentCollectionSerializerAsync.PersistObjectData throws this XpsSerializationException when the context's TargetObject is not IEnumerable. The asynchronous page-content collection serializer can only enumerate collections of PageContent items.
Solutions
- Pass an object implementing IEnumerable (enumerating PageContent) to the async collection serializer
- Use ReachPageContentSerializerAsync for individual page content items
- Inspect the SerializableObjectContext.TargetObject type before the async operation is queued
- Keep the serializer registration consistent with the object types in the document tree
Example fix
// before context = new SerializableObjectContext(fixedPage, "Pages"); // non-enumerable target // after context = new SerializableObjectContext(fixedDocument.Pages /* IEnumerable */, "Pages");
Defensive patterns
Strategy: validation
Validate before calling
if (ctx.TargetObject is not IEnumerable) throw new ArgumentException("Async collection serializer requires IEnumerable target"); Type guard
bool IsEnumerableTarget(SerializableObjectContext c) => c?.TargetObject is IEnumerable;
Prevention
- Validate context targets before pushing async operations
- Mirror sync/async target types in custom pipelines
- Test async save paths with representative document trees
When it happens
Trigger: Async XPS serialization where the SerializableObjectContext handed to PersistObjectData wraps a non-collection object (single PageContent, FixedPage, or unrelated type) instead of an enumerable PageContent collection.
Common situations: Same as the sync case but in async save paths (XpsSerializationManagerAsync): mis-wired context construction, custom serializers registered for the wrong types, refactors that changed the target's type away from a collection.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- SR.ReachSerialization_TargetNotPrintTicket
- SR.ReachSerialization_WrongPropertyTypeForPageContent
- SR.Format(SR.MustBeOfType…
- SR.MustBeOfType
- SR.MustBeOfType
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/79e5571983f26a52.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachPageContentCollectionSerializerAsync.cs:111
/// point of the serialization process.
/// </summary>
/// <param name="serializableObjectContext">
/// The context of the object to be serialized at this time.
/// </param>
internal
override
void
PersistObjectData(
SerializableObjectContext serializableObjectContext
)
{
ArgumentNullException.ThrowIfNull(serializableObjectContext);
IEnumerable enumerableObject = serializableObjectContext.TargetObject as IEnumerable;
if (enumerableObject == null)
{
throw new XpsSerializationException(SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(IEnumerable)));
}
//
// Serialize the PageContent Items contained within the collection
//
SerializePageContents(serializableObjectContext);
}
internal
override
void
EndPersistObjectData(
)
{
//
// do nothing in this stage
//
}View on GitHub (pinned to 81131a70a4)