{"record":{"id":"cf84a58698c8ce35","repo":"dotnet/wpf","slug":"sr-general-badtype-fontweightconverter","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/FontWeightConverter.cs","lineNumber":65,"sourceCode":"        /// <summary>\n        /// ConvertFrom - attempt to convert to a FontWeight 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 FontWeight.\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            FontWeight fontWeight = new FontWeight();\n            if (!FontWeights.FontWeightStringToKnownWeight(s, ci, ref fontWeight))\n                throw new FormatException(SR.Parsers_IllegalToken);\n\n            return fontWeight;\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 FontWeight,\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/FontWeightConverter.cs#L47-L83","documentation":"FontWeightConverter.ConvertFrom throws ArgumentException with General_BadType when the incoming 'value' is not a string. Only string inputs are parsed (via FontWeights.FontWeightStringToKnownWeight); other object types trigger this ArgumentException.","triggerScenarios":"Calling FontWeightConverter.ConvertFrom with a non-string object (e.g. an int like 700, a FontWeight instance, or null).","commonSituations":"Data binding or designer serialization paths where weight arrives as a number instead of a string; passing a numeric weight directly to the converter.","solutions":["Pass the weight as a string, e.g. \"Bold\" or \"700\".","If the value is numeric, call ConvertFrom(value.ToString()) or use new FontWeight((ushort)weight).","Use FontWeights.Bold and other static members directly instead of the converter."],"exampleFix":"// before\nvar weight = (FontWeight)converter.ConvertFrom(700);\n// after\nvar weight = (FontWeight)converter.ConvertFrom(\"700\");","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 { weight = (FontWeight)converter.ConvertFrom(value); } catch (ArgumentException) { weight = FontWeights.Normal; }","preventionTips":["Convert numeric weights to string before ConvertFrom.","Use FontWeights static members directly.","Check CanConvertFrom before invoking."],"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"}