{"record":{"id":"7ed7dee855e065e7","repo":"dotnet/wpf","slug":"value-must-be-of-type-glyphrun","errorCode":null,"errorMessage":"'value' must be of type 'GlyphRun'.","messagePattern":"'value' must be of type 'GlyphRun'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/FontTypeConverter.cs","lineNumber":157,"sourceCode":"            Type                                destinationType\n            )\n        {\n            ArgumentNullException.ThrowIfNull(context);\n            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertFontBegin);\n\n            if (!IsSupportedType(destinationType))\n            {\n                throw new NotSupportedException(SR.Converter_ConvertToNotSupported);\n            }\n\n            PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));\n            //\n            // Ensure that we have a valid GlyphRun instance\n            //\n            GlyphRun fontGlyphRun = (GlyphRun)value;\n            if (fontGlyphRun == null)\n            {\n                throw new ArgumentException(SR.Format(SR.MustBeOfType, \"value\", \"GlyphRun\"));\n            }\n\n            //\n            // Obtain the font serialization service from the serlialization manager.\n            //\n            IServiceProvider resourceServiceProvider = manager.ResourcePolicy;\n            XpsFontSerializationService fontService = (XpsFontSerializationService)resourceServiceProvider.GetService(typeof(XpsFontSerializationService));\n            if (fontService == null)\n            {\n                throw new XpsSerializationException(SR.ReachSerialization_NoFontService);\n            }\n\n            //\n            // Retrieve the current font subsetter\n            //\n            XpsFontSubsetter fontSubsetter = fontService.FontSubsetter;\n\n            //","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/FontTypeConverter.cs#L139-L175","documentation":"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.","triggerScenarios":"Passing null or a non-GlyphRun object (which also fails the cast) as value to FontTypeConverter.ConvertTo.","commonSituations":"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.","solutions":["Ensure the value passed to ConvertTo is a non-null GlyphRun.","Guard with 'if (value is GlyphRun gr)' before calling ConvertTo.","Check upstream formatting logic that produces the GlyphRun for null results.","Catch ArgumentException and skip/log non-convertible values during serialization."],"exampleFix":"// before\nconverter.ConvertTo(context, culture, formattedText, typeof(byte[]));\n// after\nif (value is GlyphRun glyphRun)\n    converter.ConvertTo(context, culture, glyphRun, typeof(byte[]));","handlingStrategy":"type-guard","validationCode":"if (value is not GlyphRun glyphRun || glyphRun == null)\n    throw new ArgumentException(\"'value' must be of type 'GlyphRun'.\");","typeGuard":"static bool IsGlyphRun(object v) => v is GlyphRun;","tryCatchPattern":"try { return converter.ConvertTo(context, culture, value, typeof(byte[])); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"GlyphRun\")) { log.Warn(\"Non-GlyphRun value passed to font converter\"); return null; }","preventionTips":["Always pass a non-null GlyphRun to FontTypeConverter.ConvertTo.","Use 'is GlyphRun' pattern matching before invoking the converter.","Verify upstream text layout code actually produced a GlyphRun."],"tags":["wpf","type-converter","xps","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}