{"record":{"id":"7200fc283945cbaf","repo":"dotnet/wpf","slug":"cannot-convert-to-type-fonttypeconverter","errorCode":null,"errorMessage":"Cannot convert to type.","messagePattern":"Cannot convert to type\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/FontTypeConverter.cs","lineNumber":147,"sourceCode":"        /// <returns>\n        /// The Type to convert the value parameter to.\n        /// </returns>\n        public\n        override\n        object\n        ConvertTo(\n            ITypeDescriptorContext              context,\n            System.Globalization.CultureInfo    culture,\n            object                              value,\n            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)","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/FontTypeConverter.cs#L129-L165","documentation":"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.","triggerScenarios":"Calling ConvertTo with a destinationType not whitelisted (the converter supports the types needed by XPS glyph serialization, e.g. byte[] for font embedding).","commonSituations":"Using the font converter outside XPS document serialization; design-time tools asking for string output of a GlyphRun font.","solutions":["Restrict ConvertTo calls to the destination types supported by the XPS serializer (check IsSupportedType semantics).","Ensure the context supplies an XpsSerializationManager (context.GetService) - the converter is serialization-pipeline-only.","Use framework XPS serialization (XpsDocument/XpsSerializationManager) rather than calling the converter directly.","Catch NotSupportedException and handle unsupported conversions explicitly."],"exampleFix":"// before\nvar bytes = converter.ConvertTo(context, culture, glyphRun, typeof(string));\n// after\nusing var manager = new XpsSerializationManager(new XpsPackagingPolicy(package), false);\nvar bytes = converter.ConvertTo(context, culture, glyphRun, typeof(byte[]));","handlingStrategy":"type-guard","validationCode":"if (destinationType != typeof(byte[]) /* verify supported destination */) throw new NotSupportedException();\nif (context?.GetService(typeof(XpsSerializationManager)) == null) throw new InvalidOperationException(\"serialization context required\");","typeGuard":"static bool CanConvertTo(Type t) => t == typeof(byte[]); // extend per supported set","tryCatchPattern":"try { return converter.ConvertTo(context, culture, glyphRun, destinationType); }\ncatch (NotSupportedException) { /* unsupported destination - handle */ }","preventionTips":["Only invoke FontTypeConverter inside an active XPS serialization pipeline.","Pass only destination types the XPS font serializer supports.","Ensure context.GetService(typeof(XpsSerializationManager)) is available."],"tags":["wpf","type-converter","xps","not-supported"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}