Last active
December 17, 2015 23:48
-
-
Save JohanLarsson/5691538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Enum.Extensions { | |
/// http://stackoverflow.com/a/1086742/1069200 | |
public static class EnumerationExtensions { | |
public static bool Has<T>(this System.Enum type, T value) { | |
try { | |
return (((int)(object)type & (int)(object)value) == (int)(object)value); | |
} | |
catch { | |
return false; | |
} | |
} | |
public static bool Is<T>(this System.Enum type, T value) { | |
try { | |
return (int)(object)type == (int)(object)value; | |
} | |
catch { | |
return false; | |
} | |
} | |
public static T Add<T>(this System.Enum type, T value) { | |
try { | |
return (T)(object)(((int)(object)type | (int)(object)value)); | |
} | |
catch(Exception ex) { | |
throw new ArgumentException( | |
string.Format( | |
"Could not append value from enumerated type '{0}'.", | |
typeof(T).Name | |
), ex); | |
} | |
} | |
public static T Remove<T>(this System.Enum type, T value) { | |
try { | |
return (T)(object)(((int)(object)type & ~(int)(object)value)); | |
} | |
catch (Exception ex) { | |
throw new ArgumentException( | |
string.Format( | |
"Could not remove value from enumerated type '{0}'.", | |
typeof(T).Name | |
), ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment