dotnet/wpf · error · NotSupportedException

SR.Converter_ConvertToNotSupported

Error message

SR.Converter_ConvertToNotSupported

What it means

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.

Solutions

  1. Serialize the struct as element/property syntax instead of a string attribute
  2. Use a converter or serialization surrogate that supports the target representation
  3. If you control the mcg definition, add string serialization support (CanSerializeToString) for the struct
  4. Catch NotSupportedException and use a different serialization path

Example fix

// before
string s = (string)converter.ConvertTo(ctx, culture, structValue, typeof(string)); // throws
// after
if (structValue.CanSerializeToString())
    s = (string)converter.ConvertTo(ctx, culture, structValue, typeof(string));
else
    serializer.SerializeElement(structValue);
Defensive patterns

Strategy: validation

Validate before calling

bool canSerialize = !(context?.Instance is MyStruct s) || s.CanSerializeToString();

Try / catch

try { return (string)converter.ConvertTo(ctx, culture, value, typeof(string)); }
catch (NotSupportedException) { return serializeAsElement(value); }

Prevention

When it happens

Trigger: Calling converter.ConvertTo(instance, typeof(string)) (or ConvertToString) on a generated struct whose CanSerializeToString() is false, with a non-null context and Instance.

Common situations: 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.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/ManagedStruct.cs:745

                                    }

                                    [[resource.Name]] value = ([[resource.Name]])context.Instance;

                                    return value.CanSerializeToString();
                                }


                            [[/inline]]
                                ;

                        serializationContextConvertTo =
                            [[inline]]
                                // 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);
                                    }
                                }


                            [[/inline]]
                                ;

                    }

                    csFile.WriteBlock(
                        [[inline]]

                            namespace [[resource.ManagedNamespace]]
                            {
                                /// <summary>
                                /// [[resource.Name]]Converter - Converter class for converting instances of other types to and from [[resource.Name]] instances
                                /// </summary>
                                public sealed class [[converterName]] : TypeConverter

View on GitHub (pinned to 81131a70a4)