dotnet/wpf · error · XpsSerializationException
SR.Format(SR.MustBeOfType…
Error message
SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(IEnumerable))
What it means
The PageContent collection serializer requires the serializable object context's TargetObject to implement IEnumerable; it throws XpsSerializationException(SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(IEnumerable))) otherwise. It enumerates PageContent items inside a FixedDocument, so a non-enumerable target is an invariant violation.
Solutions
- Ensure the context TargetObject is the FixedDocument's Pages/PageContent collection (IEnumerable), not a single page.
- Fix custom context construction so it passes the enumerable collection property.
- Validate with 'is IEnumerable' before creating the context.
Example fix
// before new NgcSerializableObjectContext(pageContent, ""); // single item // after new NgcSerializableObjectContext(fixedDocument.Pages, ""); // IEnumerable collection
Defensive patterns
Strategy: validation
Validate before calling
if (serializableObjectContext?.TargetObject is not IEnumerable)
throw new InvalidOperationException("PageContent serializer context target must be an IEnumerable collection"); Type guard
bool HasEnumerableTarget(NgcSerializableObjectContext ctx) => ctx?.TargetObject is IEnumerable;
Try / catch
try { SerializePageContents(context); }
catch (XpsSerializationException ex) when (ex.Message.Contains("IEnumerable")) { /* rebuild the context with the collection property */ } Prevention
- Always build serializer contexts from collection properties (e.g. FixedDocument.Pages)
- Never pass a single PageContent as a collection context target
- Assert context target types in serializer unit tests
When it happens
Trigger: Serializing a FixedDocument's PageContents where the context's TargetObject is not an IEnumerable (e.g. a single PageContent or a wrong property was pushed as the context target).
Common situations: Custom serializer contexts built by hand with the wrong TargetObject; internal context wiring broken after overriding serialization steps.
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/94041e8177908bfe.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCSerializer.cs:676
#endregion Constructor
/// <summary>
///
/// </summary>
public
override
void
SerializeObject(
Object serializedObject
)
{
IEnumerable enumerableObject = serializedObject 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(enumerableObject);
}
#region Private Methods
/// <summary>
/// This is being called to serialize the Page Content items
/// contained within the collection
/// </summary>
/// <param name="enumerableObject">
/// The context of the object to be serialized at this time.
/// </param>
privateView on GitHub (pinned to 81131a70a4)