dotnet/wpf · error · ArgumentException

SR.General_BadType

Error message

SR.General_BadType

What it means

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.

Solutions

  1. Pass the weight as a string, e.g. "Bold" or "700".
  2. If the value is numeric, call ConvertFrom(value.ToString()) or use new FontWeight((ushort)weight).
  3. Use FontWeights.Bold and other static members directly instead of the converter.

Example fix

// before
var weight = (FontWeight)converter.ConvertFrom(700);
// after
var weight = (FontWeight)converter.ConvertFrom("700");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value is string s)) throw new ArgumentException($"Expected a string, got {value?.GetType().Name}");

Type guard

bool IsConvertibleString(object v) => v is string;

Try / catch

try { weight = (FontWeight)converter.ConvertFrom(value); } catch (ArgumentException) { weight = FontWeights.Normal; }

Prevention

When it happens

Trigger: Calling FontWeightConverter.ConvertFrom with a non-string object (e.g. an int like 700, a FontWeight instance, or null).

Common situations: Data binding or designer serialization paths where weight arrives as a number instead of a string; passing a numeric weight directly to the converter.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/cf84a58698c8ce35. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FontWeightConverter.cs:65

        /// <summary>
        /// ConvertFrom - attempt to convert to a FontWeight from the given object
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// A NotSupportedException is thrown if the example object is null or is not a valid type
        /// which can be converted to a FontWeight.
        /// </exception>
        public override object ConvertFrom(ITypeDescriptorContext td, CultureInfo ci, object value)
        {
            if (null == value)
            {
                throw GetConvertFromException(value);
            }

            String s = value as string;

            if (null == s)
            {
                throw new ArgumentException(SR.Format(SR.General_BadType, "ConvertFrom"), nameof(value));
            }
            
            FontWeight fontWeight = new FontWeight();
            if (!FontWeights.FontWeightStringToKnownWeight(s, ci, ref fontWeight))
                throw new FormatException(SR.Parsers_IllegalToken);

            return fontWeight;
        }

        /// <summary>
        /// TypeConverter method implementation.
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// An NotSupportedException is thrown if the example object is null or is not a FontWeight,
        /// or if the destinationType isn't one of the valid destination types.
        /// </exception>
        /// <param name="context">ITypeDescriptorContext</param>
        /// <param name="culture">current culture (see CLR specs)</param>

View on GitHub (pinned to 81131a70a4)