~halega/+junk/sqlinstaller

« back to all changes in this revision

Viewing changes to SQLInstaller.Core/EnumerationExtensions.cs

  • Committer: sk
  • Date: 2011-09-10 05:32:36 UTC
  • Revision ID: halega@halega.com-20110910053236-1877r3p0k4a64bgx
Tags: 1.2.2
1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//-----------------------------------------------------------------------
 
2
// <copyright file="EnumerationExtensions.cs" company="JHOB Technologies, LLC">
 
3
//     Copyright © JHOB Technologies, LLC. All rights reserved.
 
4
// </copyright>
 
5
// <license>Microsoft Public License</license>
 
6
// <author>Brian Schloz</author>
 
7
//-----------------------------------------------------------------------
 
8
namespace SQLInstaller.Core
 
9
{
 
10
        using System;
 
11
 
 
12
        /// <summary>
 
13
        /// Extensions to Enum class providing some helper methods.
 
14
        /// </summary>
 
15
        public static class EnumerationExtensions
 
16
        {
 
17
        /// <summary>
 
18
        /// Method to determine whether or not an object has a given enum defined.
 
19
        /// </summary>
 
20
        /// <typeparam name="T">The enum to use.</typeparam>
 
21
        /// <param name="type">The enum instance containing the values to find.</param>
 
22
        /// <param name="value">The value to find.</param>
 
23
        /// <returns>A value indicating whether or not the object has a given enum defined.</returns>
 
24
                public static bool Has<T>(this Enum type, T value)
 
25
                {
 
26
                        try
 
27
                        {
 
28
                                return ((int)(object)type & (int)(object)value) == (int)(object)value;
 
29
                        }
 
30
                        catch
 
31
                        {
 
32
                                return false;
 
33
                        }
 
34
                }
 
35
 
 
36
        /// <summary>
 
37
        /// Method to determine whether or not an object is equal to the given enum.
 
38
        /// </summary>
 
39
        /// <typeparam name="T">The enum to use.</typeparam>
 
40
        /// <param name="type">The enum instance containing the values to compare.</param>
 
41
        /// <param name="value">The value to find.</param>
 
42
        /// <returns>A value indicating whether or not the object is equal to the given enum.</returns>
 
43
        public static bool Is<T>(this Enum type, T value)
 
44
                {
 
45
                        try
 
46
                        {
 
47
                                return (int)(object)type == (int)(object)value;
 
48
                        }
 
49
                        catch
 
50
                        {
 
51
                                return false;
 
52
                        }
 
53
                }
 
54
 
 
55
        /// <summary>
 
56
        /// Method to add a value to a given enum.
 
57
        /// </summary>
 
58
        /// <typeparam name="T">The enum to use.</typeparam>
 
59
        /// <param name="type">The enum instance containing the values to add.</param>
 
60
        /// <param name="value">The value to add.</param>
 
61
        /// <returns>The new value.</returns>
 
62
        public static T Add<T>(this Enum type, T value)
 
63
                {
 
64
                        try
 
65
                        {
 
66
                                return (T)(object)((int)(object)type | (int)(object)value);
 
67
                        }
 
68
                        catch (Exception ex)
 
69
                        {
 
70
                                throw new ArgumentException(string.Format(Resources.ErrorEnumAppend, typeof(T).Name), ex);
 
71
                        }
 
72
                }
 
73
 
 
74
        /// <summary>
 
75
        /// Method to remove a value from a given enum.
 
76
        /// </summary>
 
77
        /// <typeparam name="T">The enum to use.</typeparam>
 
78
        /// <param name="type">The enum instance containing the values to remove.</param>
 
79
        /// <param name="value">The value to remove.</param>
 
80
        /// <returns>The new value.</returns>
 
81
        public static T Remove<T>(this Enum type, T value)
 
82
                {
 
83
                        try
 
84
                        {
 
85
                                return (T)(object)((int)(object)type & ~(int)(object)value);
 
86
                        }
 
87
                        catch (Exception ex)
 
88
                        {
 
89
                                throw new ArgumentException(string.Format(Resources.ErrorEnumRemove, typeof(T).Name), ex);
 
90
                        }
 
91
                }
 
92
 
 
93
        /// <summary>
 
94
        /// Method to set a value from a given enum if a flag is set.
 
95
        /// </summary>
 
96
        /// <typeparam name="T">The enum to use.</typeparam>
 
97
        /// <param name="type">The enum instance containing the values to remove.</param>
 
98
        /// <param name="value">The value to remove.</param>
 
99
        /// <param name="doSet">A value indicating whether or not to set the enum.</param>
 
100
        /// <returns>The new value.</returns>
 
101
        public static T SetIf<T>(this Enum type, T value, bool doSet)
 
102
                {
 
103
            if (doSet)
 
104
            {
 
105
                return Add(type, value);
 
106
            }
 
107
            else
 
108
            {
 
109
                return Remove(type, value);
 
110
            }
 
111
                }
 
112
        }
 
113
}
 
 
b'\\ No newline at end of file'