lucasg/Dependencies · error · ArgumentException

Can only convert an instance of enum.

Error message

Can only convert an instance of enum.

What it means

Thrown by EnumToStringUsingDescription.ConvertTo (DependenciesGui/DependencyWindow.xaml.cs:188) when the value passed in is not an instance of an enum (its BaseType is not System.Enum). The converter then calls value.GetType().GetField(name) assuming an enum, so a non-enum value would otherwise NullRef; the guard converts that into an ArgumentException(parameterName="value").

Source

Thrown at DependenciesGui/DependencyWindow.xaml.cs:188

        {
            return (destinationType.Equals(typeof(String)));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (!destinationType.Equals(typeof(String)))
            {
                throw new ArgumentException("Can only convert to string.", "destinationType");
            }

            if (!value.GetType().BaseType.Equals(typeof(Enum)))
            {
                throw new ArgumentException("Can only convert an instance of enum.", "value");
            }

            string name = value.ToString();
            object[] attrs =
                value.GetType().GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attrs.Length > 0) ? ((DescriptionAttribute)attrs[0]).Description : name;
        }
    }

    /// <summary>
    /// User context of every dependency tree node.
    /// </summary>
    public struct DependencyNodeContext
    {
        public DependencyNodeContext(DependencyNodeContext other)
        {
            ModuleInfo = other.ModuleInfo;
            IsDummy = other.IsDummy;

View on GitHub (pinned to 1997a40000)

Solutions

  1. Ensure the type decorated with [TypeConverter(typeof(EnumToStringUsingDescription))] is genuinely an enum.
  2. Move the attribute to the correct enum type after refactoring.
  3. If you must convert a non-enum, use a dedicated converter instead of this one.

Example fix

// before: attribute left on a non-enum after refactor
[TypeConverter(typeof(EnumToStringUsingDescription))]
public struct ModuleId { public int Value; } // throws at ConvertTo
// after: only decorate enums
public enum ModuleId { [Description("Root")] Root, [Description("Child")] Child }
Defensive patterns

Strategy: type-guard

Validate before calling

// Only feed enum instances to this converter.
if (value == null || !value.GetType().IsEnum)
{
    return value?.ToString();
}
return conv.ConvertTo(null, culture, value, typeof(string));

Type guard

static bool IsEnumValue(object value) => value != null && value.GetType().IsEnum;

Try / catch

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

Prevention

When it happens

Trigger: The converter is wired (via [TypeConverter(typeof(EnumToStringUsingDescription))]) onto a type that is not an enum, or a binding feeds it a boxed non-enum value.

Common situations: Refactoring a struct/class that carried the [TypeConverter] attribute but is no longer an enum; accidental reuse of the converter on a numeric or string property.

Related errors


AI-assisted analysis of lucasg/Dependencies@1997a40000 (2026-08-13). Data as JSON: /api/errors/9fdaddf0e5582ac7. Report an issue: GitHub.