{"record":{"id":"37bcbd8f9b0f00c5","repo":"dotnet/wpf","slug":"sr-converter-converttonotsupported","errorCode":null,"errorMessage":"SR.Converter_ConvertToNotSupported","messagePattern":"SR\\.Converter_ConvertToNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/BrushConverter.cs","lineNumber":137,"sourceCode":"        /// </exception>\n        /// <param name=\"context\"> The ITypeDescriptorContext for this call. </param>\n        /// <param name=\"culture\"> The CultureInfo which is respected when converting. </param>\n        /// <param name=\"value\"> The object to convert to an instance of \"destinationType\". </param>\n        /// <param name=\"destinationType\"> The type to which this will convert the Brush instance. </param>\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            if (destinationType != null && value is Brush)\n            {\n                Brush instance = (Brush)value;\n\n                if (destinationType == typeof(string))\n                {\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                    // Delegate to the formatting/culture-aware ConvertToString method.\n                    return instance.ConvertToString(null, culture);\n                }\n            }\n\n            // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)\n            return base.ConvertTo(context, culture, value, destinationType);\n        }\n    }\n}\n","sourceCodeStart":119,"sourceCodeEnd":151,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/BrushConverter.cs#L119-L151","documentation":"BrushConverter.ConvertTo throws NotSupportedException when the target type is string but the Brush instance reports CanSerializeToString() == false. Only certain brush types (e.g. SolidColorBrush with a simple color) can be expressed as a string; others cannot round-trip and the converter refuses rather than emit a lossy value.","triggerScenarios":"Calling ConvertTo(context, culture, value, typeof(string)) where value is a Brush whose CanSerializeToString() returns false (e.g. many gradient/complex brushes) while a context with a non-null Instance is supplied.","commonSituations":"Serializing a LinearGradientBrush/RadialGradientBrush to a string for settings storage or code generation; XAML designer/serialization engines attempting string conversion of complex brushes.","solutions":["Check brush.CanSerializeToString() before calling ConvertTo and fall back to full XAML/BAML serialization for complex brushes.","Convert the brush to a resource reference or serialized form instead of a plain string.","If you only need simple brushes, restrict inputs to SolidColorBrush instances.","Handle NotSupportedException and use XamlWriter.Save as fallback."],"exampleFix":"// before\nstring s = (string)brushConverter.ConvertTo(ctx, culture, gradientBrush, typeof(string)); // throws\n// after\nstring s = gradientBrush.CanSerializeToString()\n    ? (string)brushConverter.ConvertTo(ctx, culture, gradientBrush, typeof(string))\n    : XamlWriter.Save(gradientBrush);","handlingStrategy":"validation","validationCode":"if (brush is SolidColorBrush scb) { /* safe to convert */ } else { bool convertible = ((Brush)brush).CanSerializeToString(); }","typeGuard":"bool canToString = brush is Brush b && b.CanSerializeToString();","tryCatchPattern":"try { s = (string)converter.ConvertTo(ctx, culture, brush, typeof(string)); } catch (NotSupportedException) { s = XamlWriter.Save(brush); }","preventionTips":["Check CanSerializeToString() before ConvertTo.","Use XAML serialization for gradient/complex brushes.","Avoid stringifying brushes for persistence; store resources instead.","Restrict settings models to SolidColorBrush."],"tags":["wpf","typeconverter","not-supported","serialization"],"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"}