summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/Helpers/BindingEnumHelper.cs
blob: 54e93304177931af9ff3341198ea955f8bb1aa04 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace Gestor.Application.Helpers
{
	public class BindingEnumHelper
	{
		public BindingEnumHelper()
		{
		}

		public static string ConcatenarDescricoesEnum<TEnum>(string valores)
		where TEnum : Enum
		{
			if (valores == null)
			{
				return null;
			}
			return BindingEnumHelper.ConcatenarDescricoesEnum<TEnum>(Array.ConvertAll<string, int>(valores.Split(new char[] { ',' }), new Converter<string, int>(int.Parse)));
		}

		private static string ConcatenarDescricoesEnum<TEnum>(int[] valores)
		where TEnum : Enum
		{
			StringBuilder stringBuilder = new StringBuilder();
			int[] numArray = valores;
			for (int i = 0; i < (int)numArray.Length; i++)
			{
				int num = numArray[i];
				if (!Enum.IsDefined(typeof(TEnum), num))
				{
					stringBuilder.Append("Valor desconhecido, ");
				}
				else
				{
					string str = BindingEnumHelper.ObterDescricaoEnum<TEnum>((TEnum)Enum.ToObject(typeof(TEnum), num));
					stringBuilder.Append(string.Concat(str, ", "));
				}
			}
			return stringBuilder.ToString().TrimEnd(new char[] { ',', ' ' });
		}

		private static string ObterDescricaoEnum<TEnum>(TEnum valor)
		where TEnum : Enum
		{
			DescriptionAttribute[] customAttributes = (DescriptionAttribute[])valor.GetType().GetField(valor.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
			if (customAttributes == null || customAttributes.Length == 0)
			{
				return valor.ToString();
			}
			return customAttributes[0].Description;
		}
	}
}