{"record":{"id":"f288630b12d6c503","repo":"dotnet/wpf","slug":"sr-converter-converttonotsupported-cachemodeconverter","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/CacheModeConverter.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 CacheMode instance. </param>\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            if (destinationType != null && value is CacheMode)\n            {\n                CacheMode instance = (CacheMode)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/CacheModeConverter.cs#L119-L151","documentation":"CacheModeConverter.ConvertTo throws NotSupportedException when converting to string a CacheMode whose CanSerializeToString() returns false. Not every CacheMode (e.g. some BitmapCache configurations) can be represented losslessly as a string, so the converter refuses the conversion.","triggerScenarios":"Calling ConvertTo(context, culture, value, typeof(string)) with a CacheMode instance whose CanSerializeToString() is false and a non-null context.Instance.","commonSituations":"Serializing UIElement.CacheMode values to strings for persistence or code generation; designer serialization of elements with unsupported cache settings.","solutions":["Check cacheMode.CanSerializeToString() before converting and fall back to XAML serialization otherwise.","Persist the CacheMode via XamlWriter.Save instead of a raw string.","Restrict usage to CacheMode instances known to be string-serializable.","Catch NotSupportedException and serialize with a fallback path."],"exampleFix":"// before\nstring s = (string)cacheModeConverter.ConvertTo(ctx, culture, cacheMode, typeof(string));\n// after\nstring s = cacheMode.CanSerializeToString()\n    ? (string)cacheModeConverter.ConvertTo(ctx, culture, cacheMode, typeof(string))\n    : XamlWriter.Save(cacheMode);","handlingStrategy":"validation","validationCode":"bool convertible = ctx == null || ctx.Instance == null || ((CacheMode)cacheMode).CanSerializeToString();","typeGuard":"bool canToString = cacheMode is CacheMode cm && cm.CanSerializeToString();","tryCatchPattern":"try { s = (string)converter.ConvertTo(ctx, culture, cacheMode, typeof(string)); } catch (NotSupportedException) { s = XamlWriter.Save(cacheMode); }","preventionTips":["Gate ConvertTo on CanSerializeToString().","Use XAML serialization for non-string-serializable cache modes.","Avoid string persistence of CacheMode; keep it in XAML/resources.","Test serialization with all CacheMode configurations in use."],"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"}