{"record":{"id":"0a79b3f29e083812","repo":"LykosAI/StabilityMatrix","slug":"convert-target-type-targettype-name-must-be-assignable-to","errorCode":null,"errorMessage":"Convert Target type {targetType.Name} must be assignable to {typeof(TTarget).Name}","messagePattern":"Convert Target type (.+?) must be assignable to (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs","lineNumber":55,"sourceCode":"                        nameof(NanHandling),\n                        (int)NanHandling,\n                        typeof(ReturnBehavior)\n                    )\n            };\n        }\n\n        return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource));\n    }\n\n    /// <summary>\n    /// Convert a value type to a nullable value type\n    /// </summary>\n    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)\n    {\n        if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget)))\n        {\n            // ReSharper disable once LocalizableElement\n            throw new ArgumentException(\n                $\"Convert Target type {targetType.Name} must be assignable to {typeof(TTarget).Name}\"\n            );\n        }\n\n        return (TTarget?)System.Convert.ChangeType(value, typeof(TTarget));\n    }\n\n    /// <summary>\n    /// Convert a nullable value type to a value type\n    /// </summary>\n    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)\n    {\n        if (!targetType.IsAssignableTo(typeof(TSource)))\n        {\n            // ReSharper disable once LocalizableElement\n            throw new ArgumentException(\n                $\"ConvertBack Target type {targetType.Name} must be assignable to {typeof(TSource).Name}\"\n            );","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs#L37-L73","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Match the converter's generic type parameters to the binding's target property type","Create/use a converter instance with the correct TTarget (e.g. <double,int> for int targets)","Remove the converter from bindings where no numeric conversion is needed","Add a compile-time-checked factory so mismatches surface at build time"],"exampleFix":"// before (converter <double,double> on int property)\n<TextBlock Text=\"{Binding Count, Converter={StaticResource DoubleConv}}\"/>\n// after\n<TextBlock Text=\"{Binding Count, Converter={StaticResource IntConv}}\"/>","handlingStrategy":"type-guard","validationCode":"if (targetType != typeof(double) && !typeof(double).IsAssignableFrom(targetType))\n    throw new InvalidOperationException(\"DoubleConverter requires a double target\");","typeGuard":"bool CanConvertTo<TTarget>(Type targetType) => targetType == typeof(TTarget?) || targetType.IsAssignableTo(typeof(TTarget));","tryCatchPattern":"try { converted = converter.Convert(value, targetType, param, culture); }\ncatch (ArgumentException ex) { Log.Error(ex, \"Converter target type mismatch for {Type}\", targetType); converted = null; }","preventionTips":["Match converter generic parameters to bound property types","Name converters by type (e.g. DoubleToIntConverter) to avoid misuse","Keep a central converter registry keyed by target type"],"tags":["avalonia","converter","binding","type-mismatch"],"backgroundTag":"incompatible-source-type","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}