{"record":{"id":"6442bc0e679a661b","repo":"dotnet/wpf","slug":"cannot-instantiate-a-serializer-of-the-given-type","errorCode":null,"errorMessage":"Cannot instantiate a serializer of the given type.","messagePattern":"Cannot instantiate a serializer of the given type\\.","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/MetroSerializationManager.cs","lineNumber":1076,"sourceCode":"            }\n\n            return canSerializeValue;\n        }\n\n        private \n        ReachSerializer\n        CreateReachSerializer(Type serializerType)\n        {\n            MS.Internal.Invariant.Assert(serializerType != null);\n            MS.Internal.Invariant.Assert(serializerType.IsSubclassOf(typeof(ReachSerializer)));\n\n            object[] args = new object[] { _serializationManager };\n\n            ReachSerializer result = Activator.CreateInstance(serializerType, args) as ReachSerializer;\n\n            if (result == null)\n            {\n                throw new XpsSerializationException(SR.ReachSerialization_UnableToInstantiateSerializer);\n            }\n\n            return result;\n        }\n\n        #endregion Private Methods\n\n        #region Private Data Members\n\n        private \n        PackageSerializationManager   _serializationManager;\n\n        //\n        // The following is being cached for future reference and to\n        // optimize performance\n        // 1. The Types and their clr properties\n        // 2. The Types and their corresponding dependency properties. Since,\n        //    not all types have dependency properties, that is why I am","sourceCodeStart":1058,"sourceCodeEnd":1094,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/MetroSerializationManager.cs#L1058-L1094","documentation":"SerializersCacheManager instantiates ReachSerializer types via Activator.CreateInstance and casts the result to ReachSerializer. If the created instance is not a ReachSerializer (cast returns null), it throws XpsSerializationException 'Cannot instantiate a serializer of the given type'. The serializer type registered/mapped for the CLR type does not derive from ReachSerializer.","triggerScenarios":"Serializing an object whose mapped serializer type is not a ReachSerializer subclass or whose constructor does not accept the serialization manager argument, causing the cast to ReachSerializer to yield null.","commonSituations":"Custom serializer registration mistakes (registering a type that does not inherit ReachSerializer), or framework serializer-type mapping table inconsistencies for unusual object types.","solutions":["Ensure the serializer type registered for your object type derives from ReachSerializer and has a constructor accepting the serialization manager","Check which type you are passing to SaveAsXaml and confirm it has a built-in serializer","Register/fix the custom serializer mapping"],"exampleFix":"// before\n// registered serializer does not inherit ReachSerializer\npublic class MyCustomSerializer { ... }\n// after\npublic class MyCustomSerializer : ReachSerializer { public MyCustomSerializer(SerializationManager m) : base(m) {} }","handlingStrategy":"type-guard","validationCode":"if (!typeof(ReachSerializer).IsAssignableFrom(serializerType))\n    throw new InvalidOperationException($\"{serializerType} must derive from ReachSerializer\");","typeGuard":"bool IsValidSerializerType(Type t) =>\n    t != null && typeof(ReachSerializer).IsAssignableFrom(t) &&\n    t.GetConstructor(new[] { typeof(SerializationManager) }) != null;","tryCatchPattern":"try\n{\n    manager.SaveAsXaml(obj);\n}\ncatch (XpsSerializationException ex) when (ex.Message.Contains(\"instantiate\"))\n{\n    // fix serializer registration for obj's type\n}","preventionTips":["Ensure custom serializers derive from ReachSerializer with the (SerializationManager) constructor","Test custom serializer registration early in development"],"tags":["wpf","xps","serialization","serializer-instantiation"],"backgroundTag":"invalid-config-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"}