LykosAI/StabilityMatrix · error · ArgumentException

Convert Target type must be assignable to

Error message

Convert Target type {targetType.Name} must be assignable to {typeof(TTarget).Name}

What it means

NullableDefaultNumericConverter.Convert first checks that targetType equals typeof(TTarget?) or is assignable to typeof(TTarget); if not it throws ArgumentException('Convert Target type X must be assignable to Y'). This guards against using a strongly typed converter (e.g. <double, double>) in a binding whose target property is a different type.

Solutions

  1. Match the converter's generic type parameters to the binding's target property type
  2. Create/use a converter instance with the correct TTarget (e.g. <double,int> for int targets)
  3. Remove the converter from bindings where no numeric conversion is needed
  4. Add a compile-time-checked factory so mismatches surface at build time

Example fix

// before (converter <double,double> on int property)
<TextBlock Text="{Binding Count, Converter={StaticResource DoubleConv}}"/>
// after
<TextBlock Text="{Binding Count, Converter={StaticResource IntConv}}"/>
Defensive patterns

Strategy: type-guard

Validate before calling

if (targetType != typeof(double) && !typeof(double).IsAssignableFrom(targetType))
    throw new InvalidOperationException("DoubleConverter requires a double target");

Type guard

bool CanConvertTo<TTarget>(Type targetType) => targetType == typeof(TTarget?) || targetType.IsAssignableTo(typeof(TTarget));

Try / catch

try { converted = converter.Convert(value, targetType, param, culture); }
catch (ArgumentException ex) { Log.Error(ex, "Converter target type mismatch for {Type}", targetType); converted = null; }

Prevention

When it happens

Trigger: Applying the converter in XAML to a binding whose target property type doesn't match the converter's generic TTarget — e.g. a <double,double> converter used on an int/string/Thickness property.

Common situations: Copy-pasting a converter between bindings with different property types; refactoring a property's type without updating the converter; using the converter on object-typed targets.

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 LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/0a79b3f29e083812. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs:55

                        nameof(NanHandling),
                        (int)NanHandling,
                        typeof(ReturnBehavior)
                    )
            };
        }

        return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource));
    }

    /// <summary>
    /// Convert a value type to a nullable value type
    /// </summary>
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget)))
        {
            // ReSharper disable once LocalizableElement
            throw new ArgumentException(
                $"Convert Target type {targetType.Name} must be assignable to {typeof(TTarget).Name}"
            );
        }

        return (TTarget?)System.Convert.ChangeType(value, typeof(TTarget));
    }

    /// <summary>
    /// Convert a nullable value type to a value type
    /// </summary>
    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (!targetType.IsAssignableTo(typeof(TSource)))
        {
            // ReSharper disable once LocalizableElement
            throw new ArgumentException(
                $"ConvertBack Target type {targetType.Name} must be assignable to {typeof(TSource).Name}"
            );

View on GitHub (pinned to af93d6ef57)