HandyOrg/HandyControl · error · ArgumentException

CannotConvertType

Error message

CannotConvertType

What it means

ColLayoutConverter.ConvertTo only supports converting a ColLayout instance to a string (via its ToString). Any other value type or destination type makes it throw an ArgumentException('CannotConvertType'), mirroring the .NET TypeConverter contract where an unsupported conversion is signalled with ArgumentException rather than a specific exception type.

Solutions

  1. Only call ConvertTo with a ColLayout value and destinationType == typeof(string); use ConvertFrom/Parse for the string->layout direction
  2. For other destination types, register a custom TypeConverter or handle the conversion yourself before invoking the converter
  3. Wrap the ConvertTo call in try/catch (ArgumentException) if conversion support is probed dynamically

Example fix

// before
var result = converter.ConvertTo(null, CultureInfo.InvariantCulture, "24,12,8", typeof(string)); // throws
// after
var layout = ColLayout.Parse("24,12,8");
var result = converter.ConvertTo(null, CultureInfo.InvariantCulture, layout, typeof(string));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is ColLayout && destinationType == typeof(string)) { /* safe to call ConvertTo */ }

Type guard

static bool CanConvert(object? v, Type t) => v is ColLayout && t == typeof(string);

Try / catch

try { return converter.ConvertTo(null, culture, value, typeof(string)); }
catch (ArgumentException) { return value?.ToString(); }

Prevention

When it happens

Trigger: Calling TypeDescriptor.GetConverter(colLayout).ConvertTo(value, destinationType) with destinationType other than typeof(string), or passing a value that is not a ColLayout (e.g. null, a raw string, a Col object) into ConvertTo.

Common situations: XAML or designer serialization trying to convert a ColLayout to InstanceDescriptor or another type; data-binding engines attempting non-string conversions; unit tests exercising the converter with wrong destination types.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/e82af44716fe7b02. Report an issue: GitHub.

Appendix: source

Thrown at src/Avalonia/HandyControl_Avalonia/Tools/Converter/ColLayoutConverter.cs:44

    public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
    {
        if (value == null) throw GetConvertFromException(null!);

        return value switch
        {
            string s => ColLayout.Parse(s),
            double d => new ColLayout((int) d),
            _ => new ColLayout(System.Convert.ToInt32(value, culture))
        };
    }

    public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
    {
        if (value is ColLayout layout && destinationType == typeof(string))
        {
            return layout.ToString();
        }
        throw new ArgumentException("CannotConvertType");
    }
}

View on GitHub (pinned to 2c0875ebd6)