{"record":{"id":"083979744e7ca954","repo":"dotnet/wpf","slug":"sr-general-expected-type-brush","errorCode":null,"errorMessage":"SR.General_Expected_Type (Brush)","messagePattern":"SR\\.General_Expected_Type \\(Brush\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/BrushConverter.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 Brush))\n                    {\n                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, \"Brush\"), nameof(context));\n                    }\n\n                    Brush value = (Brush)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 Brush from the given object.\n        /// </summary>\n        /// <returns>\n        /// The Brush 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/BrushConverter.cs#L49-L85","documentation":"BrushConverter.CanConvertTo throws ArgumentException when a serialization context supplies an Instance that is not a Brush. The converter can only evaluate string-serializability of Brush instances, so it validates the context object type up front via SR.General_Expected_Type. This guards the internal cast that follows.","triggerScenarios":"Calling CanConvertTo(ITypeDescriptorContext, Type) (with destinationType string) after assigning a non-Brush object to context.Instance, e.g. a raw Color, ImageSource, or wrong object placed in the descriptor context before invoking the converter during serialization.","commonSituations":"Custom serialization/xaml-generation pipelines that build a mistyped ITypeDescriptorContext; passing a surrogate object instead of the actual Brush; refactoring that changed the instance type without updating the converter context.","solutions":["Ensure context.Instance is set to the actual Brush (or a Brush-derived type) before calling CanConvertTo.","Pass a null context (or a context with null Instance) to skip the instance-specific check entirely.","Fix the caller that populates the ITypeDescriptorContext so it uses the object being converted.","If converting a non-Brush value, use that value's own TypeConverter instead of BrushConverter."],"exampleFix":"// before\nvar ctx = new TypeDescriptorContext(null, propertyDescriptor, serviceContainer) { Instance = color }; // Color, not Brush\nbool ok = converter.CanConvertTo(ctx, typeof(string));\n// after\nvar ctx = new TypeDescriptorContext(null, propertyDescriptor, serviceContainer) { Instance = solidColorBrush };\nbool ok = converter.CanConvertTo(ctx, typeof(string));","handlingStrategy":"type-guard","validationCode":"if (ctx != null && ctx.Instance != null && !(ctx.Instance is Brush)) throw new ArgumentException(\"Instance must be a Brush\");","typeGuard":"bool canQuery = ctx == null || ctx.Instance == null || ctx.Instance is Brush;","tryCatchPattern":"try { return converter.CanConvertTo(ctx, typeof(string)); } catch (ArgumentException) { /* instance was not a Brush; handle non-conversion */ return false; }","preventionTips":["Always bind context.Instance to the exact object being converted.","Prefer passing null context when you only need the default conversion answer.","Assert instance types in custom ITypeDescriptorContext implementations.","Unit-test serialization paths with the real property values."],"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"}