-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEnums.cs
More file actions
29 lines (25 loc) · 684 Bytes
/
Copy pathEnums.cs
File metadata and controls
29 lines (25 loc) · 684 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
using System.ComponentModel;
using System.Reflection;
namespace CsCodeExample;
public enum Gender
{
Neutrum = 0,
Masculinum = 1,
Femininum = 2
}
public enum Sex
{
[Description("Male desc")] // Attributes
Male = 0,
[Description("Female desc")]
Female = 1
}
public static class EnumEx
{
public static String GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString()); // Reflection
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
}