-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumExtension.cs
More file actions
37 lines (34 loc) · 1.36 KB
/
EnumExtension.cs
File metadata and controls
37 lines (34 loc) · 1.36 KB
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.Linq;
using System.ComponentModel;
using System.Reflection;
namespace Extensions
{
/// <summary>
/// This is class contains extension function(s) for enum objects
/// </summary>
public static class EnumExtension
{
/// <summary>
/// Returns description of enum value if attached with enum member through DiscriptionAttribute
/// </summary>
/// <returns>Description of enum value</returns>
/// <see cref="DescriptionAttribute"/>
public static string ToDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] descriptions = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (descriptions != null && descriptions.Length > 0) ? descriptions[0].Description : fi.Name;
}
/// <summary>
/// Returns true if enum matches any of the given values
/// </summary>
/// <param name="value">Value to match</param>
/// <param name="values">Values to match against</param>
/// <returns>Return true if matched</returns>
public static bool In(this Enum value, params Enum[] values)
{
return values.Any(v => v == value);
}
}
}