{"record":{"id":"109886f2d0c5336f","repo":"dotnet/wpf","slug":"sr-invalidcustomserialize","errorCode":null,"errorMessage":"SR.InvalidCustomSerialize","messagePattern":"SR\\.InvalidCustomSerialize","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializer.cs","lineNumber":100,"sourceCode":"        internal virtual void ConvertBamlToObject (\n            BamlRecordReader    reader,       // Current reader that is processing records\n            BamlRecord          bamlRecord,   // Record read in that triggered serializer\n            ParserContext       context)      // Context\n        {\n            throw new InvalidOperationException(SR.InvalidDeSerialize);\n        }\n\n#endif\n\n        /// <summary>\n        ///   Convert a string into a compact binary representation and write it out\n        ///   to the passed BinaryWriter.\n        /// </summary>\n        public virtual bool ConvertStringToCustomBinary (\n            BinaryWriter   writer,           // Writer into the baml stream\n            string         stringValue)      // String to convert\n        {\n            throw new InvalidOperationException(SR.InvalidCustomSerialize);\n        }\n        \n        /// <summary>\n        ///   Convert a compact binary representation of a certain object into and instance\n        ///   of that object.  The reader must be left pointing immediately after the object \n        ///   data in the underlying stream.\n        /// </summary>\n        public virtual object ConvertCustomBinaryToObject(\n            BinaryReader reader)\n        {\n            throw new InvalidOperationException(SR.InvalidCustomSerialize);\n        }            \n\n        /// <summary>\n        ///   If the object created by this serializer is stored in a dictionary, this\n        ///   method will extract the key used for this dictionary from the passed \n        ///   collection of baml records.  How the key is determined is up to the\n        ///   individual serializer.  By default, there is no key retrieved.","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializer.cs#L82-L118","documentation":"XamlSerializer.ConvertStringToCustomBinary base implementation always throws InvalidOperationException(SR.InvalidCustomSerialize). It is the public entry for converting a string value into a compact binary form for BAML; the base throws, so any serializer without the override cannot perform custom string serialization.","triggerScenarios":"BAML writing (BamlRecordWriter.WriteRecordData) encounters a value whose serializer must convert a string to custom binary, but the serializer subclass did not override ConvertStringToCustomBinary.","commonSituations":"Registering a ValueSerializer/XamlSerializer for a type to speed up BAML but only implementing the deserialize (binary->object) side; compiler/optimizer pipelines that write strings without the corresponding override.","solutions":["Override ConvertStringToCustomBinary in the serializer to write the compact binary representation to the BinaryWriter.","If custom binary conversion is not desired, remove the serializer registration so values serialize via the default path.","Mirror-implement both directions (ConvertStringToCustomBinary and the matching read) to keep BAML round-trippable."],"exampleFix":"// before\nclass MySerializer : XamlSerializer { }\n// after\nclass MySerializer : XamlSerializer {\n    public override bool ConvertStringToCustomBinary(BinaryWriter writer, string stringValue) {\n        writer.Write(stringValue); // implement real compact encoding\n        return true;\n    }\n}","handlingStrategy":"type-guard","validationCode":"var m = serializer.GetType().GetMethod(\"ConvertStringToCustomBinary\", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);\nbool canWrite = m != null && m.DeclaringType != typeof(XamlSerializer);","typeGuard":"static bool SupportsCustomBinaryWrite(XamlSerializer s) => s.GetType().GetMethod(\"ConvertStringToCustomBinary\", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic)!.DeclaringType != typeof(XamlSerializer);","tryCatchPattern":"try { ok = serializer.ConvertStringToCustomBinary(writer, value); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"CustomSerialize\")) { log.Error($\"{serializer.GetType().Name} cannot write custom binary; falling back to string record\"); // fallback: write as plain string record\n}","preventionTips":["Implement both write and read sides of custom binary serialization.","Only register custom serializers for types they fully support.","Test string -> binary -> string round trips in CI."],"tags":["wpf","baml","serialization"],"backgroundTag":"abstract-method-not-implemented","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"}