dotnet/wpf · error · NotSupportedException

SR.Converter_ConvertToNotSupported

Error message

SR.Converter_ConvertToNotSupported

What it means

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.

Solutions

  1. Check cacheMode.CanSerializeToString() before converting and fall back to XAML serialization otherwise.
  2. Persist the CacheMode via XamlWriter.Save instead of a raw string.
  3. Restrict usage to CacheMode instances known to be string-serializable.
  4. Catch NotSupportedException and serialize with a fallback path.

Example fix

// before
string s = (string)cacheModeConverter.ConvertTo(ctx, culture, cacheMode, typeof(string));
// after
string s = cacheMode.CanSerializeToString()
    ? (string)cacheModeConverter.ConvertTo(ctx, culture, cacheMode, typeof(string))
    : XamlWriter.Save(cacheMode);
Defensive patterns

Strategy: validation

Validate before calling

bool convertible = ctx == null || ctx.Instance == null || ((CacheMode)cacheMode).CanSerializeToString();

Type guard

bool canToString = cacheMode is CacheMode cm && cm.CanSerializeToString();

Try / catch

try { s = (string)converter.ConvertTo(ctx, culture, cacheMode, typeof(string)); } catch (NotSupportedException) { s = XamlWriter.Save(cacheMode); }

Prevention

When it happens

Trigger: Calling ConvertTo(context, culture, value, typeof(string)) with a CacheMode instance whose CanSerializeToString() is false and a non-null context.Instance.

Common situations: Serializing UIElement.CacheMode values to strings for persistence or code generation; designer serialization of elements with unsupported cache settings.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f288630b12d6c503. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/CacheModeConverter.cs:137

        /// </exception>
        /// <param name="context"> The ITypeDescriptorContext for this call. </param>
        /// <param name="culture"> The CultureInfo which is respected when converting. </param>
        /// <param name="value"> The object to convert to an instance of "destinationType". </param>
        /// <param name="destinationType"> The type to which this will convert the CacheMode instance. </param>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType != null && value is CacheMode)
            {
                CacheMode instance = (CacheMode)value;

                if (destinationType == typeof(string))
                {
                    // When invoked by the serialization engine we can convert to string only for some instances
                    if (context != null && context.Instance != null)
                    {
                        if (!instance.CanSerializeToString())
                        {
                            throw new NotSupportedException(SR.Converter_ConvertToNotSupported);
                        }
                    }

                    // Delegate to the formatting/culture-aware ConvertToString method.
                    return instance.ConvertToString(null, culture);
                }
            }

            // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

View on GitHub (pinned to 81131a70a4)