dotnet/wpf · error · XpsSerializationException

'serializableObjectContext.TargetObject' must be of type…

Error message

'serializableObjectContext.TargetObject' must be of type 'System.Collections.IEnumerable'.

What it means

NGCPageContentCollectionSerializerAsync.PersistObjectData expects the serializableObjectContext's TargetObject to implement IEnumerable (a collection of PageContent). If it does not, it throws XpsSerializationException stating the target must be of type IEnumerable.

Solutions

  1. Pass a PageContentCollection (or any IEnumerable of PageContent) to this serializer
  2. Use NGCPageContentSerializerAsync for a single PageContent
  3. Verify the object graph being serialized (e.g. iterate FixedDocument.Pages rather than a lone page)

Example fix

// before
manager.SaveAsXaml(pageContent); // routed to collection serializer
// after
manager.SaveAsXaml(fixedDocument.Pages); // IEnumerable of PageContent
Defensive patterns

Strategy: validation

Validate before calling

if (obj is not IEnumerable)
    throw new ArgumentException("Target must be a PageContent collection (IEnumerable)");
manager.SaveAsXaml(obj);

Type guard

bool IsPageContentCollection(object o) => o is IEnumerable;

Try / catch

try
{
    manager.SaveAsXaml(obj);
}
catch (XpsSerializationException ex) when (ex.Message.Contains("IEnumerable"))
{
    // route single PageContent to the page content serializer instead
}

Prevention

When it happens

Trigger: Asynchronously serializing a context whose TargetObject is a single PageContent or another non-collection object instead of a PageContent collection (e.g. FixedPageContent/PageContentCollection).

Common situations: Passing the wrong object to the collection serializer — usually a wiring bug in custom serialization managers or when calling SaveAsXaml with an object that gets routed to the collection 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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/47ccc1dbb7e9312c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/NGCPageContentCollectionSerializerAsync.cs:113

        /// 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)