dotnet/wpf · error · ArgumentException
SR.MustBeOfType
Error message
SR.MustBeOfType
What it means
ReachVisualSerializer.SerializeObject requires the object passed as serializedObject to be a System.Windows.Media.Visual. When the argument is null or not a Visual, the serializer throws ArgumentException with SR.MustBeOfType before any XPS serialization begins. The library validates this up front because the whole XPS serialization pipeline (visual tree walking, packaging policy) only operates on Visual-derived objects.
Solutions
- Pass a Visual-derived object (DrawingVisual, FixedPage, UIElement) as serializedObject
- Null-check the object before calling SerializeObject and handle non-Visual inputs in your own code
- If serializing non-visual data, use the appropriate serializer for that object type instead of ReachVisualSerializer
Example fix
// before
serializer.SerializeObject(myDataObject);
// after
if (myDataObject is Visual visual)
{
serializer.SerializeObject(visual);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (obj == null) throw new ArgumentNullException(nameof(obj));
if (!(obj is Visual)) throw new ArgumentException("serializedObject must be a Visual", nameof(obj)); Type guard
bool IsValidSerializedVisual(object o) => o is Visual;
Try / catch
try { serializer.SerializeObject(obj); }
catch (ArgumentException ex) when (ex.Message.Contains(typeof(Visual).Name)) { /* non-Visual argument */ } Prevention
- Always pass Visual-derived objects (UIElement, DrawingVisual, FixedPage) to ReachVisualSerializer
- Null-check arguments before serialization calls
- Keep serialization entry points strongly typed (Visual parameters) in wrapper code
When it happens
Trigger: Calling XpsSerializationManager's ReachVisualSerializer.SerializeObject with a serializedObject that is null or not derived from Visual (e.g. a raw string, a UIElement wrapper object that is not a Visual, or a forgotten return value).
Common situations: Developers plugging custom serialization into WPF's XPS printing pipeline pass a non-Visual business object instead of the DrawingVisual/FixedPage; null values from failed Visual lookups; misuse of the async/sync serializer overloads.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.MustBeOfType
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ContentType is not valid.
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0427f82ce20d702e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachVisualSerializer.cs:43
base(manager)
{
}
/// <summary>
///
/// </summary>
public
override
void
SerializeObject(
object serializedObject
)
{
Visual v = serializedObject as Visual;
if (v == null)
{
throw new ArgumentException(SR.Format(SR.MustBeOfType, "serializedObject", typeof(Visual)));
}
// The new class XpsOMSerializationManager now also interacts with this class
// the cast below is shorthand for cast to either XpsSerializationManager or XpsOMSerializationManager
// we want this to throw an InvalidCastException if it fails to mantain compatibility.
if((IXpsSerializationManager)SerializationManager != null)
{
XmlWriter pageWriter = SerializationManager.
PackagingPolicy.AcquireXmlWriterForPage();
XmlWriter resWriter = SerializationManager.
PackagingPolicy.AcquireXmlWriterForResourceDictionary();
SerializeTree(v, resWriter, pageWriter);
}
}
/// <summary>View on GitHub (pinned to 81131a70a4)