{"record":{"id":"84085648a0a9da82","repo":"dotnet/wpf","slug":"sr-format-sr-general-badtype-convertfrom","errorCode":null,"errorMessage":"SR.Format(SR.General_BadType, \"ConvertFrom\")","messagePattern":"SR\\.Format\\(SR\\.General_BadType, \"ConvertFrom\"\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IListConverters.cs","lineNumber":55,"sourceCode":"        {\n            return destinationType == typeof(string); // can only convert to string\n        }\n\n        /// <Summary>\n        /// Converts from a string.\n        /// </Summary>\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            return ConvertFromCore(td, ci, s);            \n        }\n\n        /// <Summary>\n        /// Converts to a string\n        /// </Summary>\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            if (destinationType != null)\n                return ConvertToCore(context, culture, value, destinationType);\n\n            // Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)\n            return base.ConvertTo(context, culture, value, destinationType);\n        }\n        \n        /// <summary>","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IListConverters.cs#L37-L73","documentation":"This TypeConverter's ConvertFrom accepts only string input. If value is not a string (e.g. an arbitrary object or null), an ArgumentException with SR.General_BadType formatted with 'ConvertFrom' is thrown, with 'value' as the parameter name.","triggerScenarios":"Calling converter.ConvertFrom(context, culture, value) with value that is not a string — e.g. passing a non-string object from XAML or binding code, or passing null.","commonSituations":"Custom converters used in XAML parsing with untyped markup extensions, or code feeding raw objects into type converters expecting pre-converted strings.","solutions":["Pass a string to ConvertFrom, or call ConvertToString/ToString first","Use ConvertFromInvariantString with a culture-independent string when parsing serialized values","Check value is a string (or use TypeDescriptor) before calling"],"exampleFix":"// before\nvar result = converter.ConvertFrom(context, culture, someObject);\n// after\nvar result = converter.ConvertFrom(context, culture, someObject?.ToString() ?? string.Empty);","handlingStrategy":"type-guard","validationCode":"if (value is not string s) {\n    // convert or reject before calling ConvertFrom\n    value = value?.ToString();\n}","typeGuard":"static bool IsConvertibleString(object? value) => value is string s && s.Length > 0;\n\n// usage: if (IsConvertibleString(input)) converter.ConvertFrom(ctx, culture, input);","tryCatchPattern":"try {\n    result = converter.ConvertFrom(context, culture, value);\n} catch (ArgumentException ex) when (ex.ParamName == \"value\") {\n    // handle non-string input\n}","preventionTips":["Only pass strings to ConvertFrom","Use ConvertTo to serialize, ConvertFrom to deserialize symmetrically","Check TypeConverter.CanConvertFrom(typeof(string)) first"],"tags":["wpf","typeconverter","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}