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
- Make the container type implement IEnumerable
- Use standard WPF collections so the built-in serializer matches
- 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
- Implement IEnumerable on custom containers
- Keep serializer registrations in sync between sync and async pipelines
- Use standard WPF collections
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
- SR.Format(SR.MustBeOfType…
- SR.ReachSerialization_NotXpsSerializationManagerAsync
- SR.ReachSerialization_WrongPropertyTypeForFixedDocument
- SR.ReachSerialization_WrongPropertyTypeForFixedPage
- SR.ReachSerializationAsync_NoNgcType
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>
privateView on GitHub (pinned to 81131a70a4)