dotnet/wpf · error · ArgumentException

SR.Format(SR.UnexpectedParameterType, value.GetType()…

Error message

SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(Thickness))

What it means

ThicknessConverter.ConvertTo only converts from a Thickness instance to string or InstanceDescriptor. Passing any other object type throws ArgumentException with UnexpectedParameterType, naming the actual type and typeof(Thickness).

Solutions

  1. Check 'value is Thickness' before calling ConvertTo, or branch to the correct converter (e.g. CornerRadiusConverter).
  2. Catch ArgumentException around ConvertTo for unvalidated inputs.
  3. Use TypeDescriptor.GetConverter(value) to obtain the right converter for the value's actual type.

Example fix

// before
var s = converter.ConvertTo(null, culture, cornerRadius, typeof(string)); // throws
// after
if (value is Thickness t)
    var s = (string)converter.ConvertTo(null, culture, t, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not Thickness)
    throw new ArgumentException("ConvertTo(ThicknessConverter) requires a Thickness value.", nameof(value));

Type guard

static bool IsThickness(object v) => v is Thickness;

Try / catch

try { return converter.ConvertTo(ctx, culture, value, destType); }
catch (ArgumentException ex) when (ex.ParamName == "value") { return value?.ToString(); }

Prevention

When it happens

Trigger: Calling converter.ConvertTo(context, culture, someNonThicknessObject, typeof(string)) — e.g. passing a double, Size, or CornerRadius.

Common situations: Generic serialization pipelines that funnel every value through the type's TypeConverter without checking the runtime type; refactoring that swapped CornerRadius for Thickness.

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/703d04a7c5d277cb. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs:124

        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// An ArgumentNullException is thrown if the example object is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// An ArgumentException is thrown if the object is not null and is not a Thickness,
        /// or if the destinationType isn't one of the valid destination types.
        /// </exception>
        /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param>
        /// <param name="cultureInfo"> The CultureInfo which is respected when converting. </param>
        /// <param name="value"> The Thickness to convert. </param>
        /// <param name="destinationType">The type to which to convert the Thickness instance. </param>
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
        {
            ArgumentNullException.ThrowIfNull(value);
            ArgumentNullException.ThrowIfNull(destinationType);

            if (value is not Thickness thickness)
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(Thickness)), nameof(value));

            if (destinationType == typeof(string))
                return ToString(thickness, cultureInfo);
            else if (destinationType == typeof(InstanceDescriptor))
            {
                ConstructorInfo ci = typeof(Thickness).GetConstructor(new Type[] { typeof(double), typeof(double), typeof(double), typeof(double) });
                return new InstanceDescriptor(ci, new object[] { thickness.Left, thickness.Top, thickness.Right, thickness.Bottom });
            }

            throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(Thickness), destinationType.FullName));
        }


        #endregion Public Methods

        //-------------------------------------------------------------------
        //
        //  Internal Methods

View on GitHub (pinned to 81131a70a4)