{"record":{"id":"0427f82ce20d702e","repo":"dotnet/wpf","slug":"sr-mustbeoftype-reachvisualserializer","errorCode":null,"errorMessage":"SR.MustBeOfType","messagePattern":"SR\\.MustBeOfType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachVisualSerializer.cs","lineNumber":43,"sourceCode":"        base(manager)\n        {\n        }\n\n        /// <summary>\n        ///\n        /// </summary>\n        public\n        override\n        void\n        SerializeObject(\n            object serializedObject\n            )\n        {\n            Visual v = serializedObject as Visual;\n\n            if (v == null)\n            {\n                throw new ArgumentException(SR.Format(SR.MustBeOfType, \"serializedObject\", typeof(Visual)));\n            }\n\n            //  The new class XpsOMSerializationManager now also interacts with this class\n            // the cast below is shorthand for cast to either XpsSerializationManager or XpsOMSerializationManager\n            // we want this to throw an InvalidCastException if it fails to mantain compatibility.\n            if((IXpsSerializationManager)SerializationManager != null)\n            {\n                XmlWriter pageWriter  = SerializationManager.\n                                        PackagingPolicy.AcquireXmlWriterForPage();\n\n                XmlWriter resWriter   = SerializationManager.\n                                        PackagingPolicy.AcquireXmlWriterForResourceDictionary();\n\n                SerializeTree(v, resWriter, pageWriter);\n            }\n        }\n\n        /// <summary>","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachVisualSerializer.cs#L25-L61","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nserializer.SerializeObject(myDataObject);\n// after\nif (myDataObject is Visual visual)\n{\n    serializer.SerializeObject(visual);\n}","handlingStrategy":"type-guard","validationCode":"if (obj == null) throw new ArgumentNullException(nameof(obj));\nif (!(obj is Visual)) throw new ArgumentException(\"serializedObject must be a Visual\", nameof(obj));","typeGuard":"bool IsValidSerializedVisual(object o) => o is Visual;","tryCatchPattern":"try { serializer.SerializeObject(obj); }\ncatch (ArgumentException ex) when (ex.Message.Contains(typeof(Visual).Name)) { /* non-Visual argument */ }","preventionTips":["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"],"tags":["wpf","xps","argument-exception","type-check"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}