dotnet/wpf · error · ArgumentException

SR.Format(SR.CannotConvertType, typeof(Thickness)…

Error message

SR.Format(SR.CannotConvertType, typeof(Thickness), destinationType.FullName)

What it means

After verifying the value is a Thickness, ConvertTo only supports destination types string and InstanceDescriptor. Any other destinationType throws ArgumentException with CannotConvertType, naming typeof(Thickness) and the requested destination type.

Solutions

  1. Convert only to typeof(string) or System.ComponentModel.Design.Serialization.InstanceDescriptor.
  2. For other representations, read thickness.Left/Top/Right/Bottom manually and build the target type.
  3. Catch ArgumentException for unsupported destinations and implement custom conversion.

Example fix

// before
var v = converter.ConvertTo(null, culture, thickness, typeof(Rect)); // throws
// after
var s = (string)converter.ConvertTo(null, culture, thickness, typeof(string));
var rect = Rect.Parse(s); // or construct manually from L/T/R/B
Defensive patterns

Strategy: validation

Validate before calling

if (destinationType != typeof(string) && destinationType != typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
    throw new NotSupportedException($"ThicknessConverter supports only string/InstanceDescriptor, not {destinationType}.");

Try / catch

try { return converter.ConvertTo(ctx, culture, thickness, destinationType); }
catch (ArgumentException ex) when (ex.Message.Contains("CannotConvert") || ex.Message.Contains("Thickness")) { return thickness.ToString(); }

Prevention

When it happens

Trigger: Calling ConvertTo with destinationType like typeof(double), typeof(int), or typeof(Rect) on a Thickness value.

Common situations: Serialization/export frameworks that request arbitrary target types assuming universal converter support; code that assumed a Thickness converts to an array or tuple-like type.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

        /// <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
        //
        //-------------------------------------------------------------------

        #region Internal Methods

        /// <summary>
        /// Converts <paramref name="th"/> to its string representation using the specified <paramref name="cultureInfo"/>.
        /// </summary>
        /// <param name="th">The <see cref="Thickness"/> to convert to string.</param>
        /// <param name="cultureInfo">Culture to use when formatting doubles and choosing separator.</param>

View on GitHub (pinned to 81131a70a4)