-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathEnumExtensions.cs
More file actions
56 lines (52 loc) · 1.83 KB
/
Copy pathEnumExtensions.cs
File metadata and controls
56 lines (52 loc) · 1.83 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using LEGO.AsyncAPI.Attributes;
public static class EnumExtensions
{
/// <summary>
/// Gets an attribute on an enum field value.
/// </summary>
/// <typeparam name="T">The type of the attribute to retrieve.</typeparam>
/// <param name="enumValue">The enum value.</param>
/// <returns>
/// The attribute of the specified type or null.
/// </returns>
public static T GetAttributeOfType<T>(this Enum enumValue)
where T : Attribute
{
var type = enumValue.GetType();
var memInfo = type.GetMember(enumValue.ToString()).First();
var attributes = memInfo.GetCustomAttributes<T>(false);
return attributes.FirstOrDefault();
}
/// <summary>
/// Gets the enum display name.
/// </summary>
/// <param name="enumValue">The enum value.</param>
/// <returns>
/// Use <see cref="DisplayAttribute"/> if exists.
/// Otherwise, use the standard string representation.
/// </returns>
public static string GetDisplayName(this Enum enumValue)
{
var attribute = enumValue.GetAttributeOfType<DisplayAttribute>();
return attribute == null ? enumValue.ToString() : attribute.Name;
}
public static IEnumerable<TEnum> GetFlags<TEnum>(this Enum input)
where TEnum : Enum
{
foreach (TEnum value in System.Enum.GetValues(input.GetType()))
{
if (input.HasFlag(value))
{
yield return value;
}
}
}
}
}