dotnet/wpf · error · NotSupportedException

Cannot convert to type.

Error message

Cannot convert to type.

What it means

FontTypeConverter.ConvertTo validates the destinationType against IsSupportedType and throws NotSupportedException ('Cannot convert to type.') for target types the XPS font serializer does not support. During conversion it also requires an XpsSerializationManager service from the context.

Solutions

  1. Restrict ConvertTo calls to the destination types supported by the XPS serializer (check IsSupportedType semantics).
  2. Ensure the context supplies an XpsSerializationManager (context.GetService) - the converter is serialization-pipeline-only.
  3. Use framework XPS serialization (XpsDocument/XpsSerializationManager) rather than calling the converter directly.
  4. Catch NotSupportedException and handle unsupported conversions explicitly.

Example fix

// before
var bytes = converter.ConvertTo(context, culture, glyphRun, typeof(string));
// after
using var manager = new XpsSerializationManager(new XpsPackagingPolicy(package), false);
var bytes = converter.ConvertTo(context, culture, glyphRun, typeof(byte[]));
Defensive patterns

Strategy: type-guard

Validate before calling

if (destinationType != typeof(byte[]) /* verify supported destination */) throw new NotSupportedException();
if (context?.GetService(typeof(XpsSerializationManager)) == null) throw new InvalidOperationException("serialization context required");

Type guard

static bool CanConvertTo(Type t) => t == typeof(byte[]); // extend per supported set

Try / catch

try { return converter.ConvertTo(context, culture, glyphRun, destinationType); }
catch (NotSupportedException) { /* unsupported destination - handle */ }

Prevention

When it happens

Trigger: Calling ConvertTo with a destinationType not whitelisted (the converter supports the types needed by XPS glyph serialization, e.g. byte[] for font embedding).

Common situations: Using the font converter outside XPS document serialization; design-time tools asking for string output of a GlyphRun font.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/FontTypeConverter.cs:147

        /// <returns>
        /// The Type to convert the value parameter to.
        /// </returns>
        public
        override
        object
        ConvertTo(
            ITypeDescriptorContext              context,
            System.Globalization.CultureInfo    culture,
            object                              value,
            Type                                destinationType
            )
        {
            ArgumentNullException.ThrowIfNull(context);
            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertFontBegin);

            if (!IsSupportedType(destinationType))
            {
                throw new NotSupportedException(SR.Converter_ConvertToNotSupported);
            }

            PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));
            //
            // Ensure that we have a valid GlyphRun instance
            //
            GlyphRun fontGlyphRun = (GlyphRun)value;
            if (fontGlyphRun == null)
            {
                throw new ArgumentException(SR.Format(SR.MustBeOfType, "value", "GlyphRun"));
            }

            //
            // Obtain the font serialization service from the serlialization manager.
            //
            IServiceProvider resourceServiceProvider = manager.ResourcePolicy;
            XpsFontSerializationService fontService = (XpsFontSerializationService)resourceServiceProvider.GetService(typeof(XpsFontSerializationService));
            if (fontService == null)

View on GitHub (pinned to 81131a70a4)