1
//-----------------------------------------------------------------------
2
// <copyright file="EnumerationExtensions.cs" company="JHOB Technologies, LLC">
3
// Copyright © JHOB Technologies, LLC. All rights reserved.
5
// <license>Microsoft Public License</license>
6
// <author>Brian Schloz</author>
7
//-----------------------------------------------------------------------
8
namespace SQLInstaller.Core
13
/// Extensions to Enum class providing some helper methods.
15
public static class EnumerationExtensions
18
/// Method to determine whether or not an object has a given enum defined.
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)
28
return ((int)(object)type & (int)(object)value) == (int)(object)value;
37
/// Method to determine whether or not an object is equal to the given enum.
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)
47
return (int)(object)type == (int)(object)value;
56
/// Method to add a value to a given enum.
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)
66
return (T)(object)((int)(object)type | (int)(object)value);
70
throw new ArgumentException(string.Format(Resources.ErrorEnumAppend, typeof(T).Name), ex);
75
/// Method to remove a value from a given enum.
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)
85
return (T)(object)((int)(object)type & ~(int)(object)value);
89
throw new ArgumentException(string.Format(Resources.ErrorEnumRemove, typeof(T).Name), ex);
94
/// Method to set a value from a given enum if a flag is set.
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)
105
return Add(type, value);
109
return Remove(type, value);
b'\\ No newline at end of file'