-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnumDescription.cs
40 lines (35 loc) · 1.16 KB
/
EnumDescription.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
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Web;
namespace BCA_Web_Application.Areas.Adthena.Common
{
public static class EnumDescription
{
/// <summary>
/// Retrive Description on Enum e.g
/// [Description("success")]
/// Success = 1
/// when we pass enum, it returns description
/// EnumDescription.GetDescription(Status.Success)
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
}
}