dotnet/wpf · error · XpsSerializationException

SR.MustBeOfType

Error message

SR.MustBeOfType

What it means

The async variant ReachUIElementCollectionSerializerAsync.PersistObjectData has the same requirement as the sync version: the context's TargetObject must implement IEnumerable so each contained element can be serialized asynchronously. A non-enumerable target indicates a serializer/target mismatch and triggers an XpsSerializationException naming IEnumerable.

Solutions

  1. Make the container type implement IEnumerable
  2. Use standard WPF collections so the built-in serializer matches
  3. Fix serializer registration so non-collection objects get the appropriate serializer

Example fix

// before
class MyPanel : Panel { } // stores children in a private non-enumerable custom store
// after
class MyPanel : Panel, IEnumerable { public override UIElementCollection Children { get; } public IEnumerator GetEnumerator() => Children.GetEnumerator(); }
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is not IEnumerable) throw new ArgumentException("Async collection serialization requires an IEnumerable target");

Type guard

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

Try / catch

try { asyncSerializer.SerializeObject(collection); }
catch (XpsSerializationException ex) when (ex.Message.Contains("IEnumerable")) { /* fix container type or serializer mapping */ }

Prevention

When it happens

Trigger: Asynchronously serializing an object routed to ReachUIElementCollectionSerializerAsync whose TargetObject does not implement IEnumerable — e.g. a custom non-enumerable container in the visual tree during an XpsDocument.BeginWrite operation.

Common situations: Custom collections/panels in trees saved asynchronously to XPS; mismatched serializer selection for the target type in async pipelines.

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/efb928b7e927749e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachUIElementCollectionSerializerAsync.cs:128

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

View on GitHub (pinned to 81131a70a4)