{"record":{"id":"e14b60521d45106d","repo":"dotnet/wpf","slug":"sr-format-sr-general-expected-type-nameof-pixelformat","errorCode":null,"errorMessage":"SR.Format(SR.General_Expected_Type, nameof(PixelFormat))","messagePattern":"SR\\.Format\\(SR\\.General_Expected_Type, nameof\\(PixelFormat\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/PixelFormatConverter.cs","lineNumber":78,"sourceCode":"        {\n            return o is not null ? new PixelFormat(o as string) : null;\n        }\n\n        /// <summary>\n        /// Converts a <paramref name=\"value\"/> of <see cref=\"PixelFormat\"/> to the specified <paramref name=\"destinationType\"/>.\n        /// </summary>\n        /// <param name=\"context\">Context information used for conversion.</param>\n        /// <param name=\"culture\">The culture specifier to use.</param>\n        /// <param name=\"value\"><see cref=\"PixelFormat\"/> value to convert from.</param>\n        /// <param name=\"destinationType\">Type being evaluated for conversion.</param>\n        /// <returns>A <see cref=\"string\"/> or <see cref=\"InstanceDescriptor\"/> representing the <see cref=\"PixelFormat\"/> specified by <paramref name=\"value\"/>.</returns>\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            ArgumentNullException.ThrowIfNull(destinationType);\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (value is not PixelFormat pixelFormat)\n                throw new ArgumentException(SR.Format(SR.General_Expected_Type, nameof(PixelFormat)));\n\n            if (destinationType == typeof(InstanceDescriptor))\n            {\n                ConstructorInfo ci = typeof(PixelFormat).GetConstructor(new Type[] { typeof(string) });\n                return new InstanceDescriptor(ci, new object[] { pixelFormat.ToString() });\n            }\n            else if (destinationType == typeof(string))\n            {\n                return pixelFormat.ToString();\n            }\n\n            return base.ConvertTo(context, culture, value, destinationType);\n        }\n    }\n}\n","sourceCodeStart":60,"sourceCodeEnd":94,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/PixelFormatConverter.cs#L60-L94","documentation":"PixelTypeConverter.ConvertTo requires the value being converted to be a PixelFormat instance. If value is null or not a PixelFormat, it throws ArgumentException with General_Expected_Type naming PixelFormat. Type converters are used by designers/serialization; passing the wrong object type is rejected with this message.","triggerScenarios":"Calling PixelFormatConverter.ConvertTo(context, culture, value, destinationType) with a value that is not a PixelFormat (e.g. a string like \"Bgr32\", a PixelFormats sentinel wrapper, or null) — commonly from designer/property-grid serialization pipelines.","commonSituations":"Custom designer code passing a string instead of a PixelFormat instance; binding or reflection code feeding untyped property values into ConvertTo; serialization frameworks invoking the converter with raw config values.","solutions":["Pass an actual PixelFormat instance (e.g. PixelFormats.Bgr32) to ConvertTo; parse strings via new PixelFormat(s) or TypeDescriptor first","Check value is PixelFormat before invoking the converter","If converting from string, call ConvertFrom instead of ConvertTo","Handle ArgumentException at the serialization boundary and log/report the offending type"],"exampleFix":"// before\nconverter.ConvertTo(ctx, culture, \"Bgr32\", typeof(InstanceDescriptor)); // throws\n// after\nobject val = value is string s ? new PixelFormat(s) : value;\nconverter.ConvertTo(ctx, culture, val, typeof(InstanceDescriptor));","handlingStrategy":"type-guard","validationCode":"if (value is not PixelFormat pf) throw new ArgumentException($\"Expected PixelFormat, got {value?.GetType().Name ?? \"null\"}\", nameof(value));","typeGuard":"bool IsPixelFormat(object v) => v is PixelFormat;","tryCatchPattern":"try { return converter.ConvertTo(ctx, culture, value, destinationType); }\ncatch (ArgumentException ex) { log(ex); return value?.ToString(); }","preventionTips":["Coerce strings to PixelFormat before ConvertTo","Use TypeDescriptor.GetConverter semantics symmetrically (ConvertFrom for strings)","In designer code, verify property type matches the converter","Unit-test converters with both string and typed inputs"],"tags":["wpf","typeconverter","pixelformat","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"}