Advertisement
FatalSleep

RestrictedEnum<T>

Dec 8th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. public class RestrictedEnum<T> where T : struct {
  2.     private System.Enum value;
  3.     public T Value { get { return (T)(object)value; } }
  4.  
  5.     public RestrictedEnum() {
  6.         if ( !typeof(T).IsEnum ) throw new NotSupportedException();
  7.         value = null;
  8.     }
  9.  
  10.     public bool SetFlag( Enum flag ) {
  11.         bool checkEnum = Enum.IsDefined( typeof( T ), flag );
  12.         if ( checkEnum ) value = flag;
  13.         return checkEnum;
  14.     }
  15.  
  16.     public bool HasFlag( Enum flag ) {
  17.         return ( Enum.IsDefined( typeof( T ), flag ) ) ? Enum.Equals( value, flag ) : false;
  18.     }
  19.  
  20.     public T Parse() {
  21.         return (T)(object)value;
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement