dotnet/wpf · error · ArgumentException

SR.General_Expected_Type (Brush)

Error message

SR.General_Expected_Type (Brush)

What it means

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.

Solutions

  1. Ensure context.Instance is set to the actual Brush (or a Brush-derived type) before calling CanConvertTo.
  2. Pass a null context (or a context with null Instance) to skip the instance-specific check entirely.
  3. Fix the caller that populates the ITypeDescriptorContext so it uses the object being converted.
  4. If converting a non-Brush value, use that value's own TypeConverter instead of BrushConverter.

Example fix

// before
var ctx = new TypeDescriptorContext(null, propertyDescriptor, serviceContainer) { Instance = color }; // Color, not Brush
bool ok = converter.CanConvertTo(ctx, typeof(string));
// after
var ctx = new TypeDescriptorContext(null, propertyDescriptor, serviceContainer) { Instance = solidColorBrush };
bool ok = converter.CanConvertTo(ctx, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (ctx != null && ctx.Instance != null && !(ctx.Instance is Brush)) throw new ArgumentException("Instance must be a Brush");

Type guard

bool canQuery = ctx == null || ctx.Instance == null || ctx.Instance is Brush;

Try / catch

try { return converter.CanConvertTo(ctx, typeof(string)); } catch (ArgumentException) { /* instance was not a Brush; handle non-conversion */ return false; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/083979744e7ca954. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/BrushConverter.cs:67

        /// <summary>
        /// Returns true if this type converter can convert to the given type.
        /// </summary>
        /// <returns>
        /// bool - True if this converter can convert to the provided type, false if not.
        /// </returns>
        /// <param name="context"> The ITypeDescriptorContext for this call. </param>
        /// <param name="destinationType"> The Type being queried for support. </param>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                // When invoked by the serialization engine we can convert to string only for some instances
                if (context != null && context.Instance != null)
                {
                    if (!(context.Instance is Brush))
                    {
                        throw new ArgumentException(SR.Format(SR.General_Expected_Type, "Brush"), nameof(context));
                    }

                    Brush value = (Brush)context.Instance;

                    return value.CanSerializeToString();
                }

                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// Attempts to convert to a Brush from the given object.
        /// </summary>
        /// <returns>
        /// The Brush which was constructed.

View on GitHub (pinned to 81131a70a4)