{"record":{"id":"d93a019354b1fba7","repo":"dotnet/wpf","slug":"sr-converter-converttonotsupported-managedstruct","errorCode":null,"errorMessage":"SR.Converter_ConvertToNotSupported","messagePattern":"SR\\.Converter_ConvertToNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/ManagedStruct.cs","lineNumber":745,"sourceCode":"                                    }\n\n                                    [[resource.Name]] value = ([[resource.Name]])context.Instance;\n\n                                    return value.CanSerializeToString();\n                                }\n\n\n                            [[/inline]]\n                                ;\n\n                        serializationContextConvertTo =\n                            [[inline]]\n                                // When invoked by the serialization engine we can convert to string only for some instances\n                                if (context != null && context.Instance != null)\n                                {\n                                    if (!instance.CanSerializeToString())\n                                    {\n                                        throw new NotSupportedException(SR.Converter_ConvertToNotSupported);\n                                    }\n                                }\n\n\n                            [[/inline]]\n                                ;\n\n                    }\n\n                    csFile.WriteBlock(\n                        [[inline]]\n\n                            namespace [[resource.ManagedNamespace]]\n                            {\n                                /// <summary>\n                                /// [[resource.Name]]Converter - Converter class for converting instances of other types to and from [[resource.Name]] instances\n                                /// </summary>\n                                public sealed class [[converterName]] : TypeConverter","sourceCodeStart":727,"sourceCodeEnd":763,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/ManagedStruct.cs#L727-L763","documentation":"This SR resource backs a NotSupportedException thrown by the generated struct TypeConverter's ConvertTo(string) path (ManagedStruct.cs template) when the struct instance's CanSerializeToString() returns false. Only generated structs that can round-trip to a string support string conversion; others refuse it.","triggerScenarios":"Calling converter.ConvertTo(instance, typeof(string)) (or ConvertToString) on a generated struct whose CanSerializeToString() is false, with a non-null context and Instance.","commonSituations":"XAML/serialization engine attempting to write a struct value as an attribute string when the struct requires element syntax; a converter registered against a struct type that lacks a string representation.","solutions":["Serialize the struct as element/property syntax instead of a string attribute","Use a converter or serialization surrogate that supports the target representation","If you control the mcg definition, add string serialization support (CanSerializeToString) for the struct","Catch NotSupportedException and use a different serialization path"],"exampleFix":"// before\nstring s = (string)converter.ConvertTo(ctx, culture, structValue, typeof(string)); // throws\n// after\nif (structValue.CanSerializeToString())\n    s = (string)converter.ConvertTo(ctx, culture, structValue, typeof(string));\nelse\n    serializer.SerializeElement(structValue);","handlingStrategy":"validation","validationCode":"bool canSerialize = !(context?.Instance is MyStruct s) || s.CanSerializeToString();","typeGuard":null,"tryCatchPattern":"try { return (string)converter.ConvertTo(ctx, culture, value, typeof(string)); }\ncatch (NotSupportedException) { return serializeAsElement(value); }","preventionTips":["Prefer element syntax for structs without string round-trips","Check CanSerializeToString before requesting string conversion"],"tags":["wpf","typeconverter","serialization","notsupported"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}