forked from hermixy/guitar-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnumToStringConverter.cs
32 lines (27 loc) · 976 Bytes
/
EnumToStringConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using Avalonia.Data.Converters;
using Humanizer;
namespace GuitarConfigurator.NetCore;
public class EnumToStringConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value == null)
return null;
var valueType = value!.GetType();
var fieldInfo = valueType.GetField(value!.ToString()!, BindingFlags.Static | BindingFlags.Public)!;
var attributes = (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
return attributes[0].Description;
}
return fieldInfo.Name.Humanize();
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}