dotnet/wpf · error · XpsSerializationException

SR.MustBeOfType

Error message

SR.MustBeOfType

What it means

ReachPageContentCollectionSerializer.PersistObjectData throws this XpsSerializationException when the SerializableObjectContext's TargetObject does not implement IEnumerable. This serializer only knows how to walk a collection of PageContent items, so the target being serialized must be an enumerable collection; anything else is a programming error in the serialization tree.

Solutions

  1. Ensure the object passed to the collection serializer's SerializeObject implements IEnumerable and enumerates PageContent items
  2. If serializing a single page, use ReachPageContentSerializer instead of the collection serializer
  3. Check how the SerializableObjectContext was built — the TargetObject should come from iterating the parent document's Pages property
  4. Verify custom serializer registration so each object type maps to the correct serializer

Example fix

// before
serializer.SerializeObject(fixedPage); // wrong serializer: collection serializer gets non-IEnumerable target
// after
var pageContentCollection = fixedDocument.Pages; // IEnumerable<PageContent>
serializer.SerializeObject(pageContentCollection);
Defensive patterns

Strategy: validation

Validate before calling

if (obj is not IEnumerable) throw new ArgumentException("Target must be an IEnumerable of PageContent");

Type guard

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

Prevention

When it happens

Trigger: Calling SerializeObject/PersistObjectData with a ReachSerializerContext whose TargetObject is not an IEnumerable — e.g. passing a single PageContent, a FixedPage, or a non-collection object to the collection serializer instead of a PageContentCollection (such as a FixedPageContent sequence from a FixedDocument).

Common situations: Wiring up a custom XPS serialization pipeline and registering the collection serializer for the wrong object type; serializing a FixedDocument but handing the context the document rather than its Pages collection; type changes after refactoring where the target stopped implementing IEnumerable.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachPageContentCollectionSerializer.cs:65

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

        #endregion Internal Methods

        #region Private Methods

        /// <summary>
        /// This is being called to serialize the Page Content items
        /// contained within the collection
        /// </summary>
        /// <param name="serializableObjectContext">
        /// The context of the object to be serialized at this time.

View on GitHub (pinned to 81131a70a4)