{"record":{"id":"732064005104e370","repo":"lucasg/Dependencies","slug":"can-only-convert-to-string","errorCode":null,"errorMessage":"Can only convert to string.","messagePattern":"Can only convert to string\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DependenciesGui/DependencyWindow.xaml.cs","lineNumber":183,"sourceCode":"        {\n            return (sourceType.Equals(typeof(Enum)));\n        }\n\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\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","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/lucasg/Dependencies/blob/1997a40000b77bd3326cbc33672a7b9f78bb23f3/DependenciesGui/DependencyWindow.xaml.cs#L165-L201","documentation":"Thrown by EnumToStringUsingDescription.ConvertTo (DependenciesGui/DependencyWindow.xaml.cs:183) when the WPF binding infrastructure requests a conversion to any type other than System.String. This TypeConverter exists solely to render enums as their [Description] text in the UI, so non-string targets are rejected with ArgumentException(parameterName=\"destinationType\").","triggerScenarios":"A XAML binding or PropertyGrid asks the converter to produce a non-string representation (e.g. via a custom control requesting the enum's underlying type, or an invalid Binding converter parameter).","commonSituations":"Custom XAML templates that re-template an enum-bound control and request an exotic target type; programmatic misuse of the converter against a TypeDescriptor chain.","solutions":["Check the binding/template that targets the enum and ensure it only needs a string representation (the converter's CanConvertTo already reports only String).","If a non-string conversion is genuinely needed, use a different converter rather than this description-rendering one.","Catch ArgumentException at the call site if the converter is invoked reflectively and degrade to value.ToString()."],"exampleFix":"// before: invoking the converter for an arbitrary type\nvar text = conv.ConvertTo(null, culture, enumValue, typeof(int)); // throws\n// after: guard with CanConvertTo\nvar targetType = typeof(int);\nvar text = conv.CanConvertTo(null, targetType)\n    ? conv.ConvertTo(null, culture, enumValue, targetType)\n    : enumValue.ToString();","handlingStrategy":"type-guard","validationCode":"// Consult CanConvertTo before ConvertTo; it already reports only String.\nif (!conv.CanConvertTo(null, destinationType))\n{\n    return value.ToString(); // graceful fallback instead of ArgumentException\n}\nreturn conv.ConvertTo(null, culture, value, destinationType);","typeGuard":"bool CanConvertToString(Type t) => t == typeof(string);","tryCatchPattern":"try { return conv.ConvertTo(context, culture, value, destinationType); }\ncatch (ArgumentException) when (!destinationType.Equals(typeof(string)))\n{\n    return value?.ToString();\n}","preventionTips":["TypeConverter users should always honour CanConvertTo/CanConvertFrom rather than calling ConvertTo unconditionally.","Reserve EnumToStringUsingDescription for enum->string display bindings only."],"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"}