EnumToStringConverter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.ComponentModel;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using Avalonia.Data.Converters;
  5. namespace InABox.Avalonia.Converters;
  6. public class EnumToStringConverter : IValueConverter
  7. {
  8. public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  9. {
  10. if (value is null)
  11. return "";
  12. if (value.GetType().IsEnum)
  13. return ((Enum)value).ToDescription();
  14. throw new ArgumentException("Convert:Value must be an enum.");
  15. }
  16. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  17. {
  18. if (value is null)
  19. return targetType.GetTypeInfo().IsValueType ? Activator.CreateInstance(targetType) : null;
  20. if (value is EnumDescription _enumDescription)
  21. return _enumDescription.Value;
  22. throw new ArgumentException("ConvertBack:EnumDescription must be an enum.");
  23. }
  24. }
  25. public record EnumDescription
  26. {
  27. public object Value { get; set; }
  28. public string Description { get; set; }
  29. public string Help { get; set; }
  30. public override string ToString()
  31. {
  32. return Description;
  33. }
  34. }
  35. public static class EnumUtils
  36. {
  37. public static IEnumerable<EnumDescription> ToDescriptions(Type t)
  38. {
  39. if (!t.IsEnum)
  40. throw new ArgumentException($"{nameof(t)} must be an enum type");
  41. return Enum.GetValues(t).Cast<Enum>().Select(ToDescription).ToList();
  42. }
  43. public static EnumDescription ToDescription(this Enum value)
  44. {
  45. string description;
  46. string help = null;
  47. var attributes = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
  48. if (attributes.Any())
  49. {
  50. description = (attributes.First() as DescriptionAttribute)?.Description;
  51. }
  52. else
  53. {
  54. TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
  55. description = ti.ToTitleCase(ti.ToLower(value.ToString().Replace("_", " ")));
  56. }
  57. if(description.IndexOf(';') is var index && index != -1)
  58. {
  59. help = description.Substring(index + 1);
  60. description = description.Substring(0, index);
  61. }
  62. return new EnumDescription() { Value = value, Description = description, Help = help };
  63. }
  64. // public static string GetStringValue(Enum value)
  65. // {
  66. // string output = null;
  67. // Type type = value.GetType();
  68. // FieldInfo fi = type.GetField(value.ToString());
  69. // if (fi.GetCustomAttributes(typeof(StringValue),
  70. // false) is StringValue[] { Length: > 0 } attrs)
  71. // {
  72. // output = attrs[0].Value;
  73. // }
  74. // return output;
  75. // }
  76. public static T? Parse<T>(string input) where T : struct
  77. {
  78. if (!typeof(T).IsEnum)
  79. {
  80. throw new ArgumentException("Generic Type 'T' must be an Enum.");
  81. }
  82. if (string.IsNullOrEmpty(input)) return null;
  83. if (Enum.GetNames(typeof(T)).Any(
  84. e => e.Trim().ToUpperInvariant() == input.Trim().ToUpperInvariant()))
  85. {
  86. return (T)Enum.Parse(typeof(T), input, true);
  87. }
  88. return null;
  89. }
  90. }