{"record":{"id":"b37c65abc3446d36","repo":"dotnet/wpf","slug":"sr-general-badtype-fontstyleconverter","errorCode":null,"errorMessage":"SR.General_BadType","messagePattern":"SR\\.General_BadType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FontStyleConverter.cs","lineNumber":65,"sourceCode":"        /// <summary>\n        /// ConvertFrom - attempt to convert to a FontStyle from the given object\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">\n        /// A NotSupportedException is thrown if the example object is null or is not a valid type\n        /// which can be converted to a FontStyle.\n        /// </exception>\n        public override object ConvertFrom(ITypeDescriptorContext td, CultureInfo ci, object value)\n        {\n            if (null == value)\n            {\n                throw GetConvertFromException(value);\n            }\n\n            String s = value as string;\n\n            if (null == s)\n            {\n                throw new ArgumentException(SR.Format(SR.General_BadType, \"ConvertFrom\"), nameof(value));\n            }\n            \n            FontStyle fontStyle = new FontStyle();\n            if (!FontStyles.FontStyleStringToKnownStyle(s, ci, ref fontStyle))\n                throw new FormatException(SR.Parsers_IllegalToken);\n\n            return fontStyle;\n        }\n\n        /// <summary>\n        /// TypeConverter method implementation.\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">\n        /// An NotSupportedException is thrown if the example object is null or is not a FontStyle,\n        /// or if the destinationType isn't one of the valid destination types.\n        /// </exception>\n        /// <param name=\"context\">ITypeDescriptorContext</param>\n        /// <param name=\"culture\">current culture (see CLR specs)</param>","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FontStyleConverter.cs#L47-L83","documentation":"FontStyleConverter.ConvertFrom throws ArgumentException with General_BadType when the incoming 'value' is not a string. The converter parses only string input (via FontStyles.FontStyleStringToKnownStyle); any other object type is rejected with this ArgumentException.","triggerScenarios":"Calling FontStyleConverter.ConvertFrom with a non-string object such as a number, FontStyle instance, or null.","commonSituations":"Binding pipelines or XAML markup extension scenarios where the value was expected to be a string but arrives as another type (e.g. already-typed FontStyle, boxed int).","solutions":["Convert the value to string before calling ConvertFrom (value.ToString()).","Check CanConvertFrom(value is string) before invoking.","If the value is already a FontStyle, use it directly without conversion."],"exampleFix":"// before\nvar style = (FontStyle)converter.ConvertFrom(someObject);\n// after\nvar style = (FontStyle)converter.ConvertFrom(someObject?.ToString() ?? \"Normal\");","handlingStrategy":"type-guard","validationCode":"if (!(value is string s)) throw new ArgumentException($\"Expected a string, got {value?.GetType().Name}\");","typeGuard":"bool IsConvertibleString(object v) => v is string;","tryCatchPattern":"try { style = (FontStyle)converter.ConvertFrom(value); } catch (ArgumentException) { style = FontStyles.Normal; }","preventionTips":["Check CanConvertFrom first.","Use FontStyles static members in code.","Normalize values to strings before conversion."],"tags":["wpf","typeconverter","argumentexception"],"backgroundTag":"type-mismatch","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"}