~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/EnumExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.ComponentModel;
 
4
using System.Linq;
 
5
using System.Reflection;
 
6
 
 
7
namespace ServiceStack.Common
 
8
{
 
9
    public static class EnumExtensions
 
10
    {
 
11
        /// <summary>
 
12
        /// Gets the textual description of the enum if it has one. e.g.
 
13
        /// 
 
14
        /// <code>
 
15
        /// enum UserColors
 
16
        /// {
 
17
        ///     [Description("Bright Red")]
 
18
        ///     BrightRed
 
19
        /// }
 
20
        /// UserColors.BrightRed.ToDescription();
 
21
        /// </code>
 
22
        /// </summary>
 
23
        /// <param name="enum"></param>
 
24
        /// <returns></returns>
 
25
        public static string ToDescription(this Enum @enum) 
 
26
        {
 
27
            var type = @enum.GetType();
 
28
            var memInfo = type.GetMember(@enum.ToString());
 
29
            if (memInfo != null && memInfo.Length > 0)
 
30
            {
 
31
                var attrs = memInfo[0].GetCustomAttributes(
 
32
                    typeof(DescriptionAttribute),
 
33
                    false);
 
34
 
 
35
                if (attrs != null && attrs.Length > 0)
 
36
                    return ((DescriptionAttribute)attrs[0]).Description;
 
37
            }
 
38
 
 
39
            return @enum.ToString();
 
40
        }
 
41
 
 
42
        public static List<string> ToList(this Enum @enum)
 
43
        {
 
44
#if !SILVERLIGHT4
 
45
            return new List<string>(Enum.GetNames(@enum.GetType()));
 
46
#else
 
47
            return @enum.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
 
48
#endif
 
49
        }
 
50
 
 
51
        public static bool Has<T>(this Enum type, T value)
 
52
        {
 
53
            try
 
54
            {
 
55
                return (((int)(object)type & (int)(object)value) == (int)(object)value);
 
56
            }
 
57
            catch
 
58
            {
 
59
                return false;
 
60
            }
 
61
        }
 
62
 
 
63
        public static bool Is<T>(this Enum type, T value)
 
64
        {
 
65
            try
 
66
            {
 
67
                return (int)(object)type == (int)(object)value;
 
68
            }
 
69
            catch
 
70
            {
 
71
                return false;
 
72
            }
 
73
        }
 
74
 
 
75
 
 
76
        public static T Add<T>(this Enum type, T value)
 
77
        {
 
78
            try
 
79
            {
 
80
                return (T)(object)(((int)(object)type | (int)(object)value));
 
81
            }
 
82
            catch (Exception ex)
 
83
            {
 
84
                throw new ArgumentException(
 
85
                    string.Format(
 
86
                        "Could not append value from enumerated type '{0}'.",
 
87
                        typeof(T).Name
 
88
                        ), ex);
 
89
            }
 
90
        }
 
91
 
 
92
        public static T Remove<T>(this Enum type, T value)
 
93
        {
 
94
            try
 
95
            {
 
96
                return (T)(object)(((int)(object)type & ~(int)(object)value));
 
97
            }
 
98
            catch (Exception ex)
 
99
            {
 
100
                throw new ArgumentException(
 
101
                    string.Format(
 
102
                        "Could not remove value from enumerated type '{0}'.",
 
103
                        typeof(T).Name
 
104
                        ), ex);
 
105
            }
 
106
        }
 
107
 
 
108
    }
 
109
 
 
110
}
 
 
b'\\ No newline at end of file'