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
- 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.
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
- 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.
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
- SR.General_Expected_Type (CacheMode)
- General_Expected_Type (ImageSource)
- SR.Converter_ConvertToNotSupported
- SR.Converter_ConvertToNotSupported
- SR.Converter_ConvertToNotSupported
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)