2
using System.Collections.Generic;
3
using System.ComponentModel;
5
using System.Reflection;
7
namespace ServiceStack.Common
9
public static class EnumExtensions
12
/// Gets the textual description of the enum if it has one. e.g.
17
/// [Description("Bright Red")]
20
/// UserColors.BrightRed.ToDescription();
23
/// <param name="enum"></param>
24
/// <returns></returns>
25
public static string ToDescription(this Enum @enum)
27
var type = @enum.GetType();
28
var memInfo = type.GetMember(@enum.ToString());
29
if (memInfo != null && memInfo.Length > 0)
31
var attrs = memInfo[0].GetCustomAttributes(
32
typeof(DescriptionAttribute),
35
if (attrs != null && attrs.Length > 0)
36
return ((DescriptionAttribute)attrs[0]).Description;
39
return @enum.ToString();
42
public static List<string> ToList(this Enum @enum)
45
return new List<string>(Enum.GetNames(@enum.GetType()));
47
return @enum.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
51
public static bool Has<T>(this Enum type, T value)
55
return (((int)(object)type & (int)(object)value) == (int)(object)value);
63
public static bool Is<T>(this Enum type, T value)
67
return (int)(object)type == (int)(object)value;
76
public static T Add<T>(this Enum type, T value)
80
return (T)(object)(((int)(object)type | (int)(object)value));
84
throw new ArgumentException(
86
"Could not append value from enumerated type '{0}'.",
92
public static T Remove<T>(this Enum type, T value)
96
return (T)(object)(((int)(object)type & ~(int)(object)value));
100
throw new ArgumentException(
102
"Could not remove value from enumerated type '{0}'.",
b'\\ No newline at end of file'