dotnet/wpf · error · NotSupportedException

SR.Converter_ConvertToNotSupported

Error message

SR.Converter_ConvertToNotSupported

What it means

TransformConverter.ConvertTo can serialize only Transform instances whose CanSerializeToString() returns true (e.g., simple matrix/translate transforms). When the serialization engine requests a string conversion for an instance that has no string representation, the converter throws NotSupportedException(SR.Converter_ConvertToNotSupported) instead of producing a lossy or invalid string.

Solutions

  1. Convert only transforms that report CanSerializeToString() == true; check it before calling ConvertTo
  2. Serialize the transform via XamlWriter or by decomposing it into a MatrixTransform (Matrix.Value) instead of the type converter
  3. Round-trip complex transforms as a Matrix (matrix.ToString()) rather than relying on TransformConverter

Example fix

// before
var s = (string)converter.ConvertTo(ctx, culture, transform, typeof(string)); // throws for complex transforms
// after
var s = transform.CanSerializeToString()
    ? (string)converter.ConvertTo(ctx, culture, transform, typeof(string))
    : transform.Value.ToString(System.Globalization.CultureInfo.InvariantCulture);
Defensive patterns

Strategy: validation

Validate before calling

if (!transform.CanSerializeToString())
    throw new NotSupportedException($"{transform.GetType().Name} has no string representation; use XAML or Matrix serialization.");

Type guard

static bool IsStringSerializable(Transform t) => t != null && t.CanSerializeToString();

Try / catch

try { s = (string)converter.ConvertTo(ctx, culture, transform, typeof(string)); }
catch (NotSupportedException) { s = transform.Value.ToString(CultureInfo.InvariantCulture); }

Prevention

When it happens

Trigger: Calling ConvertTo(context, culture, value, typeof(string)) — directly or via TypeDescriptor.GetConverter(...).ConvertToString — where the instance's CanSerializeToString() is false, such as complex transform instances the converter cannot represent as a string.

Common situations: Serializing a TransformGroup or animation-bearing transform to XAML/string form via the type converter; code generators or designers assuming every Freezable converts to string; storing transforms in string-based config formats.

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/e6a9f4fa48d92ec4. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TransformConverter.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 Transform instance. </param>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType != null && value is Transform)
            {
                Transform instance = (Transform)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)