summaryrefslogtreecommitdiff
path: root/Gestor.Model/Gestor.Model.Converter/EnumDescriptionTypeConverter.cs
blob: f114fcc3c0be674f5cec534cea462076aa41868f (plain)
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
33
34
35
36
37
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;

namespace Gestor.Model.Converter;

public class EnumDescriptionTypeConverter : EnumConverter
{
	public EnumDescriptionTypeConverter(Type type)
		: base(type)
	{
	}

	public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
	{
		if (destinationType != typeof(string))
		{
			return base.ConvertTo(context, culture, value, destinationType);
		}
		if (value == null)
		{
			return string.Empty;
		}
		FieldInfo field = value.GetType().GetField(value.ToString());
		if (field == null)
		{
			return string.Empty;
		}
		DescriptionAttribute[] array = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
		if (array.Length == 0 || string.IsNullOrEmpty(array[0].Description))
		{
			return value.ToString();
		}
		return array[0].Description;
	}
}