C# Enum 简易权限设计 使用FlagsAttribute属性

基本權限設計:

/// <summary>
/// 權限列舉
/// </summary>
[FlagsAttribute]
public enum Permissions
{
    [Description("未設定")]
    None = 0,
    [Description("建立")]
    Create = 1,
    [Description("讀取")]
    Read = 2,
    [Description("更新")]
    Update = 4,
    [Description("刪除")]
    Delete = 8,
    [Description("所有功能")]
    All = Create | Read | Update | Delete
}

 

 

 

技巧一:利用 2 的次方值,來作為列舉值以便日後做位元運算  ( & AND | OR、 ^ XOR)。

 

技巧二:現有權限中,加入新權限動作。(OR 運算)

Permissions permission = Permissions.None;
permission = permission | Permissions.Create;
MessageBox.Show(permission.ToString());
// Result: Create

 

 

 

 

技巧三:加入【FlagsAttribute】標籤

Permissions permission = Permissions.None;
permission = permission | Permissions.Create;
MessageBox.Show(permission.ToString());
// Result: Create ([FlagsAttribute]不影響結果)
 
permission = permission | Permissions.Read;
MessageBox.Show(permission.ToString());
// Result: 3 (未加入[FlagsAttribute])
// Result: Create, Read (加入[FlagsAttribute])

 

 

 

 

技巧四:現有權限中,移除某個權限動作。( XOR 與 AND 運算應用)

Permissions permission = Permissions.None;
permission = permission | Permissions.Create;
MessageBox.Show(permission.ToString());
// Result: Create
 
permission = permission | Permissions.Read;
permission = permission | Permissions.Update;
MessageBox.Show(permission.ToString());
// Result: Create, Read, Update
 
permission = (permission & (Permissions.All ^ Permissions.Read));
MessageBox.Show(permission.ToString());
// Result: Create, Update

 

 

 

 

技巧五:判斷現在權限中,是否擁有某個權限 (AND 運算)

Permissions permission = Permissions.None;
permission = permission | Permissions.Create;
MessageBox.Show(permission.ToString());
// Result: Create
 
permission = permission | Permissions.Read;
permission = permission | Permissions.Update;
MessageBox.Show(permission.ToString());
// Result: Create, Read, Update
 
permission = (permission & (Permissions.All ^ Permissions.Read));
MessageBox.Show(permission.ToString());
// Result: Create, Update
 
bool IsCreatable = false;
if((permission & Permissions.Create) == Permissions.Create)
{
    IsCreatable = true;
}
else
{
    IsCreatable = false;
}
MessageBox.Show(IsCreatable.ToString());
// Result: True

 

 

 

 

簡易權限類別設計:

   1:  /// <summary>
   2:  /// 我的權限
   3:  /// </summary>
   4:  public class MyPermission
   5:  {
   6:      private Permissions _permission;
   7:      private bool _isCreatable = false;
   8:      private bool _isReadable = false;
   9:      private bool _isUpdatable = false;
  10:      private bool _isDeletable = false;
  11:   
  12:      /// <summary>
  13:      /// 權限
  14:      /// </summary>
  15:      public Permissions Permission
  16:      {
  17:          get { return _permission; }
  18:          set
  19:          {
  20:              _permission = value;
  21:   
  22:              IsCreatable = ((value & Permissions.Create) == Permissions.Create) ? true : false;
  23:              IsReadable = ((value & Permissions.Read) == Permissions.Read) ? true : false;
  24:              IsUpdatable = ((value & Permissions.Update) == Permissions.Update) ? true : false;
  25:              IsDeletable = ((value & Permissions.Delete) == Permissions.Delete) ? true : false;
  26:          }
  27:      }
  28:   
  29:      /// <summary>
  30:      /// 是否可建立?
  31:      /// </summary>
  32:      public bool IsCreatable
  33:      {
  34:          get { return _isCreatable; }
  35:          set
  36:          {
  37:              _isCreatable = value;
  38:   
  39:              if (value)
  40:              {
  41:                  AddPermissionFlag(Permissions.Create);
  42:              }
  43:              else
  44:              {
  45:                  RemovePermissionFlag(Permissions.Create);
  46:              }
  47:          }
  48:      }
  49:   
  50:      /// <summary>
  51:      /// 是否可讀?
  52:      /// </summary>
  53:      public bool IsReadable
  54:      {
  55:          get { return _isReadable; }
  56:          set 
  57:          {
  58:              _isReadable = value;
  59:   
  60:              if (value)
  61:              {
  62:                  AddPermissionFlag(Permissions.Read);
  63:              }
  64:              else
  65:              {
  66:                  RemovePermissionFlag(Permissions.Read);
  67:              }
  68:          }
  69:      }
  70:   
  71:      /// <summary>
  72:      /// 是否可新增?
  73:      /// </summary>
  74:      public bool IsUpdatable
  75:      {
  76:          get { return _isUpdatable; }
  77:          set
  78:          {
  79:              _isUpdatable = value;
  80:   
  81:              if (value)
  82:              {
  83:                  AddPermissionFlag(Permissions.Update);
  84:              }
  85:              else
  86:              {
  87:                  RemovePermissionFlag(Permissions.Update);
  88:              }
  89:          }
  90:      }
  91:   
  92:      /// <summary>
  93:      /// 是否可刪除?
  94:      /// </summary>
  95:      public bool IsDeletable
  96:      {
  97:          get { return _isDeletable; }
  98:          set
  99:          {
 100:              _isDeletable = value;
 101:   
 102:              if (value)
 103:              {
 104:                  AddPermissionFlag(Permissions.Delete);
 105:              }
 106:              else
 107:              {
 108:                  RemovePermissionFlag(Permissions.Delete);
 109:              }
 110:          }
 111:      }
 112:   
 113:      /// <summary>
 114:      /// 現有權限中增加某個權限
 115:      /// </summary>
 116:      /// <param name="addFlag">某個權限</param>
 117:      public void AddPermissionFlag (Permissions addFlag)
 118:      {
 119:          _permission = _permission | addFlag;
 120:      }
 121:   
 122:      /// <summary>
 123:      /// 現有權限中移除某個權限
 124:      /// </summary>
 125:      /// <param name="removeFlag">某權限</param>
 126:      public void RemovePermissionFlag (Permissions removeFlag)
 127:      {
 128:          _permission = (_permission & (Permissions.All ^ removeFlag));    
 129:      }
 130:  }

 

 

 

 

簡易權限類別使用:

   1:  MyPermission mp = new MyPermission();
   2:   
   3:  mp.Permission = Permissions.Create;
   4:  // Create
   5:  MessageBox.Show(mp.Permission.ToString());
   6:   
   7:  mp.AddPermissionFlag(Permissions.Read);
   8:  // Create, Read
   9:  MessageBox.Show(mp.Permission.ToString());
  10:   
  11:  mp.IsUpdatable = true;
  12:  // Create, Read, Update
  13:  MessageBox.Show(mp.Permission.ToString());
  14:   
  15:  mp.IsDeletable = true;
  16:  // All
  17:  MessageBox.Show(mp.Permission.ToString());
  18:   
  19:  mp.RemovePermissionFlag(Permissions.Delete);
  20:  // Create, Read, Update
  21:  MessageBox.Show(mp.Permission.ToString());
  22:   
  23:  mp.IsUpdatable = false;
  24:  // Create, Read
  25:  MessageBox.Show(mp.Permission.ToString());

 

 

from:http://www.dotblogs.com.tw/chhuang/archive/2008/04/26/3514.aspx

 

 

你可能感兴趣的:(attribute)