-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnumHelper.cs
26 lines (25 loc) · 972 Bytes
/
EnumHelper.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
using NullGuard;
using System.ComponentModel;
using System.Reflection;
namespace Hspi
{
[NullGuard(ValidationFlags.Arguments | ValidationFlags.NonPublic)]
internal static class EnumHelper
{
/// <summary>
/// Returns the value of the DescriptionAttribute if the specified Enum value has one.
/// If not, returns the ToString() representation of the Enum value.
/// </summary>
/// <param name="value">The Enum to get the description for</param>
/// <returns></returns>
public static string GetDescription(System.Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
}