dotnet/wpf · error · XpsSerializationException
SR.Format(SR.MustBeOfType…
Error message
SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(IEnumerable))
What it means
XpsSerializationException thrown by NGCUIElementCollectionSerializerAsync.PersistObjectData when the serialization context's TargetObject cannot be cast to IEnumerable. This serializer is only designed to serialize collections (specifically collections of page/UI elements) during XPS document writing, so it rejects any object it was handed that is not enumerable.
Solutions
- Ensure the object passed to the serializer's PersistObjectData has a TargetObject that implements System.Collections.IEnumerable (e.g. a UIElementCollection).
- If serializing a single element, wrap it in a collection or route it to the appropriate single-object serializer instead of NGCUIElementCollectionSerializerAsync.
- Verify custom serializer registration/mapping does not bind this collection serializer to non-collection types.
- Debug serializableObjectContext.TargetObject.GetType() at the throw site to identify which non-enumerable type leaked in.
Example fix
// before serializerContext.TargetObject = mySingleVisual; // not IEnumerable // after serializerContext.TargetObject = myVisualCollection; // implements IEnumerable
Defensive patterns
Strategy: validation
Validate before calling
if (context?.TargetObject is not System.Collections.IEnumerable)
throw new ArgumentException("TargetObject must implement IEnumerable", nameof(context)); Type guard
bool IsEnumerableTarget(SerializableObjectContext c) => c?.TargetObject is System.Collections.IEnumerable;
Try / catch
try { serializer.PersistObjectData(context, ...); }
catch (XpsSerializationException ex) when (ex.Message.Contains("IEnumerable"))
{ /* log type of context.TargetObject and fall back to single-object serializer */ } Prevention
- Always hand collection serializers an object implementing IEnumerable.
- Unit-test serializer registration mappings against the types they will receive.
- Log TargetObject.GetType() before serialization in debug builds.
- Wrap single elements in a containing collection when a collection serializer is required.
When it happens
Trigger: Calling XpsDocument/XpsSerializationManager APIs with a SerializableObjectContext whose TargetObject is a non-IEnumerable object (e.g. a single UIElement or arbitrary object) instead of a collection such as UIElementCollection or a FixedDocument/PageContent collection.
Common situations: Passing the wrong object to a custom serializer or document sequence during XPS export; registering this serializer for a type it was not designed for; refactoring printing code so a single element instead of its parent collection reaches the serializer.
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/deded941f454e84e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCUIElementCollectionSerializerAsync.cs:127
/// 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
//
SerializeUIElements(serializableObjectContext);
}
#endregion Internal Methods
#region Private Methods
/// <summary>
/// This is being called to serialize the Page Content items
/// contained within the collection
/// </summary>
privateView on GitHub (pinned to 81131a70a4)