{"record":{"id":"4ead28f5fbfac9db","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-typeof-thickness","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, typeof(Thickness), destinationType.FullName)","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, typeof\\(Thickness\\), destinationType\\.FullName\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs","lineNumber":134,"sourceCode":"        /// <param name=\"value\"> The Thickness to convert. </param>\n        /// <param name=\"destinationType\">The type to which to convert the Thickness instance. </param>\n        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n            ArgumentNullException.ThrowIfNull(destinationType);\n\n            if (value is not Thickness thickness)\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(Thickness)), nameof(value));\n\n            if (destinationType == typeof(string))\n                return ToString(thickness, cultureInfo);\n            else if (destinationType == typeof(InstanceDescriptor))\n            {\n                ConstructorInfo ci = typeof(Thickness).GetConstructor(new Type[] { typeof(double), typeof(double), typeof(double), typeof(double) });\n                return new InstanceDescriptor(ci, new object[] { thickness.Left, thickness.Top, thickness.Right, thickness.Bottom });\n            }\n\n            throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(Thickness), destinationType.FullName));\n        }\n\n\n        #endregion Public Methods\n\n        //-------------------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //-------------------------------------------------------------------\n\n        #region Internal Methods\n\n        /// <summary>\n        /// Converts <paramref name=\"th\"/> to its string representation using the specified <paramref name=\"cultureInfo\"/>.\n        /// </summary>\n        /// <param name=\"th\">The <see cref=\"Thickness\"/> to convert to string.</param>\n        /// <param name=\"cultureInfo\">Culture to use when formatting doubles and choosing separator.</param>","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs#L116-L152","documentation":"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.","triggerScenarios":"Calling ConvertTo with destinationType like typeof(double), typeof(int), or typeof(Rect) on a Thickness value.","commonSituations":"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.","solutions":["Convert only to typeof(string) or System.ComponentModel.Design.Serialization.InstanceDescriptor.","For other representations, read thickness.Left/Top/Right/Bottom manually and build the target type.","Catch ArgumentException for unsupported destinations and implement custom conversion."],"exampleFix":"// before\nvar v = converter.ConvertTo(null, culture, thickness, typeof(Rect)); // throws\n// after\nvar s = (string)converter.ConvertTo(null, culture, thickness, typeof(string));\nvar rect = Rect.Parse(s); // or construct manually from L/T/R/B","handlingStrategy":"validation","validationCode":"if (destinationType != typeof(string) && destinationType != typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))\n    throw new NotSupportedException($\"ThicknessConverter supports only string/InstanceDescriptor, not {destinationType}.\");","typeGuard":null,"tryCatchPattern":"try { return converter.ConvertTo(ctx, culture, thickness, destinationType); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"CannotConvert\") || ex.Message.Contains(\"Thickness\")) { return thickness.ToString(); }","preventionTips":["Restrict serializer requests to supported destination types.","Build custom representations (Rect, tuples) from Left/Top/Right/Bottom directly.","Document converter capability in shared serialization helpers."],"tags":["wpf","type-converter","unsupported-conversion"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}