{"record":{"id":"181b24138f91f8e1","repo":"dotnet/wpf","slug":"general-expected-type-imagesource","errorCode":null,"errorMessage":"General_Expected_Type (ImageSource)","messagePattern":"General_Expected_Type \\(ImageSource\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ImageSourceConverter.cs","lineNumber":55,"sourceCode":"\n        /// <summary>\n        /// Returns true if this type converter can convert to the given type.\n        /// </summary>\n        /// <returns>\n        /// bool - True if this converter can convert to the provided type, false if not.\n        /// </returns>\n        /// <param name=\"context\"> The ITypeDescriptorContext for this call. </param>\n        /// <param name=\"destinationType\"> The Type being queried for support. </param>\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            if (destinationType == typeof(string))\n            {\n                // When invoked by the serialization engine we can convert to string only for some instances\n                if (context != null && context.Instance != null)\n                {\n                    if (!(context.Instance is ImageSource))\n                    {\n                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, \"ImageSource\"), nameof(context));\n                    }\n\n                    ImageSource value = (ImageSource)context.Instance;\n\n                    return value.CanSerializeToString();\n                }\n\n                return true;\n            }\n\n            return base.CanConvertTo(context, destinationType);\n        }\n\n        /// <summary>\n        /// Attempts to convert to a ImageSource from the given object.\n        /// </summary>\n        /// <returns>\n        /// The ImageSource which was constructed.","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ImageSourceConverter.cs#L37-L73","documentation":"ImageSourceConverter.CanConvertTo throws ArgumentException with 'General_Expected_Type' (expecting ImageSource) when the conversion context's Instance is not an ImageSource. The converter only supports serializing instances that are ImageSources, so a foreign context instance is rejected as an invalid argument.","triggerScenarios":"TypeConverter infrastructure invoking CanConvertTo/serialization on an ImageSourceConverter with an ITypeDescriptorContext whose Instance is not an ImageSource — typically when the converter is mistakenly associated with or invoked for a non-ImageSource object.","commonSituations":"Custom XAML serialization or designer tooling that passes the wrong context instance; misuse of ImageSourceConverter on a property whose declaring object is not an ImageSource.","solutions":["Ensure the converter is only used on ImageSource instances (e.g. BitmapImage, DrawingImage)","Pass a correct ITypeDescriptorContext whose Instance is the ImageSource","Check `context.Instance is ImageSource` before invoking the converter"],"exampleFix":"// before\nconverter.CanConvertTo(badContext, typeof(string)); // context.Instance is not ImageSource\n// after\nif (badContext?.Instance is ImageSource)\n{\n    converter.CanConvertTo(badContext, typeof(string));\n}","handlingStrategy":"type-guard","validationCode":"if (context?.Instance is not ImageSource)\n    throw new ArgumentException(\"Converter context instance must be an ImageSource\", nameof(context));","typeGuard":"bool IsImageSourceContext(ITypeDescriptorContext ctx) => ctx?.Instance is ImageSource;","tryCatchPattern":"try { return converter.CanConvertTo(context, typeof(string)); }\ncatch (ArgumentException) { return false; /* not an ImageSource instance */ }","preventionTips":["Associate ImageSourceConverter only with ImageSource-typed members","Pass the ImageSource as the context Instance in custom serialization","Null-check context before relying on Instance"],"tags":["wpf","typeconverter","argument-exception","imagesource"],"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"}