{"record":{"id":"703d04a7c5d277cb","repo":"dotnet/wpf","slug":"sr-format-sr-unexpectedparametertype-value-gettype-typeof-703d04","errorCode":null,"errorMessage":"SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(Thickness))","messagePattern":"SR\\.Format\\(SR\\.UnexpectedParameterType, value\\.GetType\\(\\), typeof\\(Thickness\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs","lineNumber":124,"sourceCode":"        /// </returns>\n        /// <exception cref=\"ArgumentNullException\">\n        /// An ArgumentNullException is thrown if the example object is null.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        /// An ArgumentException is thrown if the object is not null and is not a Thickness,\n        /// or if the destinationType isn't one of the valid destination types.\n        /// </exception>\n        /// <param name=\"typeDescriptorContext\"> The ITypeDescriptorContext for this call. </param>\n        /// <param name=\"cultureInfo\"> The CultureInfo which is respected when converting. </param>\n        /// <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","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs#L106-L142","documentation":"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).","triggerScenarios":"Calling converter.ConvertTo(context, culture, someNonThicknessObject, typeof(string)) — e.g. passing a double, Size, or CornerRadius.","commonSituations":"Generic serialization pipelines that funnel every value through the type's TypeConverter without checking the runtime type; refactoring that swapped CornerRadius for Thickness.","solutions":["Check 'value is Thickness' before calling ConvertTo, or branch to the correct converter (e.g. CornerRadiusConverter).","Catch ArgumentException around ConvertTo for unvalidated inputs.","Use TypeDescriptor.GetConverter(value) to obtain the right converter for the value's actual type."],"exampleFix":"// before\nvar s = converter.ConvertTo(null, culture, cornerRadius, typeof(string)); // throws\n// after\nif (value is Thickness t)\n    var s = (string)converter.ConvertTo(null, culture, t, typeof(string));","handlingStrategy":"type-guard","validationCode":"if (value is not Thickness)\n    throw new ArgumentException(\"ConvertTo(ThicknessConverter) requires a Thickness value.\", nameof(value));","typeGuard":"static bool IsThickness(object v) => v is Thickness;","tryCatchPattern":"try { return converter.ConvertTo(ctx, culture, value, destType); }\ncatch (ArgumentException ex) when (ex.ParamName == \"value\") { return value?.ToString(); }","preventionTips":["Dispatch to the converter matching the value's runtime type.","Use TypeDescriptor.GetConverter(value) instead of hardcoding converters.","Add unit tests for serialization of each WPF value type."],"tags":["wpf","type-converter","argument"],"backgroundTag":"type-mismatch","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"}