dotnet/wpf · error · XpsSerializationException
SR.MustBeOfType
Error message
SR.MustBeOfType
What it means
ReachUIElementCollectionSerializer.PersistObjectData only knows how to serialize objects implementing IEnumerable, because it enumerates the target to serialize each contained UI element. If the context's TargetObject is not enumerable, the serializer was matched to the wrong object type and an XpsSerializationException naming IEnumerable is thrown.
Solutions
- Make the container implement IEnumerable (and ideally ICollection/UIElementCollection semantics) so it is serializable
- Use standard WPF collections (UIElementCollection, VisualCollection) in serialized trees
- Ensure the serializer selection maps the object to the correct serializer type rather than the collection serializer
Example fix
// before
class MyContainer : Visual { public List<Visual> Items = new List<Visual>(); }
// after
class MyContainer : Visual, IEnumerable { public List<Visual> Items = new List<Visual>(); public IEnumerator GetEnumerator() => Items.GetEnumerator(); } Defensive patterns
Strategy: type-guard
Validate before calling
if (obj is not IEnumerable) throw new ArgumentException("Target must implement IEnumerable to use the UI element collection serializer"); Type guard
bool IsEnumerableCollection(object o) => o is IEnumerable;
Try / catch
try { serializer.SerializeObject(collection); }
catch (XpsSerializationException ex) when (ex.Message.Contains("IEnumerable")) { /* use a different serializer or fix the container type */ } Prevention
- Only register the collection serializer for IEnumerable targets
- Implement IEnumerable on custom containers
- Prefer standard WPF collections in serialized trees
When it happens
Trigger: Serializing an object whose type resolved to ReachUIElementCollectionSerializer (e.g. a UIElementCollection-like container) but whose runtime TargetObject does not implement IEnumerable — typically a custom collection or container type that mimics the serialized shape without implementing IEnumerable.
Common situations: Custom panels/collections substituted for standard WPF collections in a serialized visual tree; type-substitution bugs where a serializer is selected for a non-enumerable object.
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.Format(SR.MustBeOfType…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0392385d5b2ba41b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachUIElementCollectionSerializer.cs:88
/// 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)