dotnet/wpf · error · ArgumentException

'value' must be of type 'GlyphRun'.

Error message

'value' must be of type 'GlyphRun'.

What it means

FontTypeConverter.ConvertTo casts the value to GlyphRun and, when the cast yields null, throws ArgumentException with "'value' must be of type 'GlyphRun'." ConvertTo only converts GlyphRun instances to their serialized font representation.

Solutions

  1. Ensure the value passed to ConvertTo is a non-null GlyphRun.
  2. Guard with 'if (value is GlyphRun gr)' before calling ConvertTo.
  3. Check upstream formatting logic that produces the GlyphRun for null results.
  4. Catch ArgumentException and skip/log non-convertible values during serialization.

Example fix

// before
converter.ConvertTo(context, culture, formattedText, typeof(byte[]));
// after
if (value is GlyphRun glyphRun)
    converter.ConvertTo(context, culture, glyphRun, typeof(byte[]));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not GlyphRun glyphRun || glyphRun == null)
    throw new ArgumentException("'value' must be of type 'GlyphRun'.");

Type guard

static bool IsGlyphRun(object v) => v is GlyphRun;

Try / catch

try { return converter.ConvertTo(context, culture, value, typeof(byte[])); }
catch (ArgumentException ex) when (ex.Message.Contains("GlyphRun")) { log.Warn("Non-GlyphRun value passed to font converter"); return null; }

Prevention

When it happens

Trigger: Passing null or a non-GlyphRun object (which also fails the cast) as value to FontTypeConverter.ConvertTo.

Common situations: Custom XPS serialization code calling the converter with a TextBlock/FontFamily/FormattedText instead of the underlying GlyphRun, or with a null value when a glyph run was never produced.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

            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)
            {
                throw new XpsSerializationException(SR.ReachSerialization_NoFontService);
            }

            //
            // Retrieve the current font subsetter
            //
            XpsFontSubsetter fontSubsetter = fontService.FontSubsetter;

            //

View on GitHub (pinned to 81131a70a4)