{"record":{"id":"9fdaddf0e5582ac7","repo":"lucasg/Dependencies","slug":"can-only-convert-an-instance-of-enum","errorCode":null,"errorMessage":"Can only convert an instance of enum.","messagePattern":"Can only convert an instance of enum\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DependenciesGui/DependencyWindow.xaml.cs","lineNumber":188,"sourceCode":"        {\n            return (destinationType.Equals(typeof(String)));\n        }\n\n        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)\n        {\n            return base.ConvertFrom(context, culture, value);\n        }\n\n        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)\n        {\n            if (!destinationType.Equals(typeof(String)))\n            {\n                throw new ArgumentException(\"Can only convert to string.\", \"destinationType\");\n            }\n\n            if (!value.GetType().BaseType.Equals(typeof(Enum)))\n            {\n                throw new ArgumentException(\"Can only convert an instance of enum.\", \"value\");\n            }\n\n            string name = value.ToString();\n            object[] attrs =\n                value.GetType().GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);\n            return (attrs.Length > 0) ? ((DescriptionAttribute)attrs[0]).Description : name;\n        }\n    }\n\n    /// <summary>\n    /// User context of every dependency tree node.\n    /// </summary>\n    public struct DependencyNodeContext\n    {\n        public DependencyNodeContext(DependencyNodeContext other)\n        {\n            ModuleInfo = other.ModuleInfo;\n            IsDummy = other.IsDummy;","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/lucasg/Dependencies/blob/1997a40000b77bd3326cbc33672a7b9f78bb23f3/DependenciesGui/DependencyWindow.xaml.cs#L170-L206","documentation":"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\").","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the type decorated with [TypeConverter(typeof(EnumToStringUsingDescription))] is genuinely an enum.","Move the attribute to the correct enum type after refactoring.","If you must convert a non-enum, use a dedicated converter instead of this one."],"exampleFix":"// before: attribute left on a non-enum after refactor\n[TypeConverter(typeof(EnumToStringUsingDescription))]\npublic struct ModuleId { public int Value; } // throws at ConvertTo\n// after: only decorate enums\npublic enum ModuleId { [Description(\"Root\")] Root, [Description(\"Child\")] Child }","handlingStrategy":"type-guard","validationCode":"// Only feed enum instances to this converter.\nif (value == null || !value.GetType().IsEnum)\n{\n    return value?.ToString();\n}\nreturn conv.ConvertTo(null, culture, value, typeof(string));","typeGuard":"static bool IsEnumValue(object value) => value != null && value.GetType().IsEnum;","tryCatchPattern":"try { return conv.ConvertTo(context, culture, value, typeof(string)); }\ncatch (ArgumentException) { return value?.ToString(); }","preventionTips":["Prefer value.GetType().IsEnum over the BaseType==typeof(Enum) check used internally - IsEnum covers all enum kinds.","After refactoring, audit any [TypeConverter(typeof(EnumToStringUsingDescription))] attributes to ensure they still sit on enums."],"tags":["wpf","typeconverter","binding","gui"],"backgroundTag":null,"analyzedSha":"1997a40000b77bd3326cbc33672a7b9f78bb23f3","analyzedAt":"2026-08-13T18:14:41.696Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}