{"record":{"id":"233c10c6aec1dfe7","repo":"dotnet/wpf","slug":"sr-general-badtype","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/FontStretchConverter.cs","lineNumber":65,"sourceCode":"        /// <summary>\n        /// ConvertFrom - attempt to convert to a FontStretch 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 FontStretch.\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            FontStretch fontStretch = new FontStretch();\n            if (!FontStretches.FontStretchStringToKnownStretch(s, ci, ref fontStretch))\n                throw new FormatException(SR.Parsers_IllegalToken);\n\n            return fontStretch;\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 FontStretch,\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/FontStretchConverter.cs#L47-L83","documentation":"TypeConverter.ConvertFrom for FontStretch throws ArgumentException with General_BadType when the incoming 'value' is not a string (value as string yields null). The converter only accepts string input and converts it via FontStretches.FontStretchStringToKnownStretch; any non-string object is rejected before parsing.","triggerScenarios":"Calling FontStretchConverter.ConvertFrom with a non-string object, e.g. a number, FontStretch instance, or null boxed object.","commonSituations":"Data binding or XAML designer scenarios where a property value arrives as a non-string (e.g. an int or already-converted FontStretch) and is routed through the converter.","solutions":["Pass a string to ConvertFrom (e.g. value.ToString() if the object has a meaningful textual form).","If the value is already a FontStretch, skip the converter or call CanConvertFrom first.","Use TypeDescriptor/Convert.ChangeType so the value is normalized to string before conversion."],"exampleFix":"// before\nvar stretch = (FontStretch)converter.ConvertFrom(someFontStretchObject);\n// after\nvar stretch = (FontStretch)converter.ConvertFrom(someFontStretchObject.ToString());","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 { stretch = (FontStretch)converter.ConvertFrom(value); } catch (ArgumentException) { stretch = FontStretches.Normal; }","preventionTips":["Check CanConvertFrom before calling ConvertFrom.","Coerce bound values to string before conversion.","Use FontStretches static members in code instead of the converter."],"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"}