{"record":{"id":"c8573dbe9c13eb65","repo":"dotnet/wpf","slug":"sr-general-expected-type-cachemode","errorCode":null,"errorMessage":"SR.General_Expected_Type (CacheMode)","messagePattern":"SR\\.General_Expected_Type \\(CacheMode\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/CacheModeConverter.cs","lineNumber":67,"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 CacheMode))\n                    {\n                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, \"CacheMode\"), nameof(context));\n                    }\n\n                    CacheMode value = (CacheMode)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 CacheMode from the given object.\n        /// </summary>\n        /// <returns>\n        /// The CacheMode which was constructed.","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/CacheModeConverter.cs#L49-L85","documentation":"CacheModeConverter.CanConvertTo throws ArgumentException when the context's Instance is not a CacheMode. The converter only knows how to judge string-serializability of CacheMode instances (e.g. BitmapCache), so it validates the instance type with SR.General_Expected_Type before casting.","triggerScenarios":"Calling CanConvertTo(ITypeDescriptorContext, Type) (to string) with a context whose Instance is not a CacheMode — e.g. a Brush, UIElement, or arbitrary object placed in the serialization context.","commonSituations":"Serialization pipelines reusing a generic type descriptor context across properties and forgetting to swap Instance; converting UIElement.CacheMode with the wrong object bound to the context.","solutions":["Set context.Instance to the actual CacheMode-derived object (e.g. BitmapCache) before calling CanConvertTo.","Pass a null context or null Instance to bypass the instance check.","Fix the context-populating caller to bind the object being converted.","Use the value's own TypeConverter when the value is not a CacheMode."],"exampleFix":"// before\nctx.Instance = visual; // wrong object\nconverter.CanConvertTo(ctx, typeof(string));\n// after\nctx.Instance = new BitmapCache(); // CacheMode instance\nconverter.CanConvertTo(ctx, typeof(string));","handlingStrategy":"type-guard","validationCode":"if (ctx != null && ctx.Instance != null && !(ctx.Instance is CacheMode)) throw new ArgumentException(\"Instance must be a CacheMode\");","typeGuard":"bool canQuery = ctx == null || ctx.Instance == null || ctx.Instance is CacheMode;","tryCatchPattern":"try { return converter.CanConvertTo(ctx, typeof(string)); } catch (ArgumentException) { return false; }","preventionTips":["Bind context.Instance to the CacheMode value being serialized.","Don't reuse one descriptor context across differently-typed properties.","Pass null context for default conversion behavior.","Add assertions in custom context implementations."],"tags":["wpf","typeconverter","argument-exception","serialization"],"backgroundTag":"incompatible-source-type","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"}