forked from PassivePicasso/ThunderKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumExtensions.cs
More file actions
32 lines (28 loc) · 907 Bytes
/
Copy pathEnumExtensions.cs
File metadata and controls
32 lines (28 loc) · 907 Bytes
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.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThunderKit.Common
{
public static class EnumExtensions
{
public static string GetDescription<T>(this T enumValue) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
return null;
var description = enumValue.ToString();
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
if (fieldInfo != null)
{
var attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (attrs != null && attrs.Length > 0)
{
description = ((DescriptionAttribute)attrs[0]).Description;
}
}
return description;
}
}
}