0
1
2
4
8
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8192
-- 2的n次方 tinyint类型就够用了
-- 0 无权限
-- 1 可读(read)
-- 2 可新增(insert)
-- 4 可修改(update)
-- 8 可删除(delete)
-- 16 可审核
...
权限的组合
read + insert = 1 + 2 = 3
read + insert + delete = 1 + 2 + 8 = 11
read + update + delete = 1 + 4 + 8 = 13
select 1 | 2 权限的加法 就是逻辑 [ 或 ]运算 -- 结果: 3
select 3 & ( ~ 1) 权限的减法, 使用 [ 与 ]运算 + [ 非 ]运算来实现 -- 结果: 2
select 1 | 13 一次添加n个权限 -- 结果: 13
select 13 & ( ~ 11) 一次减去n个权限 -- 结果:4
select ( 3 & 2) 权限的判断 -- 结果:2
-- 例子
Select * From Customer Where ( @Status is null Or [ Status ] & @Status = [ Status ])
int [] power = new int [] { 1, 2, 4, 8, 16, 32, 64 };
int value = 126;
for ( int i = 0; i < power.Length; i ++)
{
if ((value & power [ i ]) != 0)
{
Console.WriteLine("有power [ {0} ] ={ 1}所代表的权限", i, power [ i ]);
}
}
-- 读取权限
private int [] GetPermission( int PermissionSum)
{
List < int > list = new List < int >();
int [] table = { 1, 2, 4, 8, 16, 32, 64};
for ( int i = table.Length - 1; i >- 1; i -- )
{
if ( table [ i ] == PermissionSum)
{
list. Add( table [ i ]);
break;
}
if ( table [ i ] > PermissionSum)
{
continue;
}
PermissionSum -= table [ i ];
list. Add( table [ i ]);
}
return list.ToArray();
}
http: //www.cnblogs.com /zhuqil /archive / 2010 / 04 / 02 /Permission.html
-- 2的n次方 tinyint类型就够用了
-- 0 无权限
-- 1 可读(read)
-- 2 可新增(insert)
-- 4 可修改(update)
-- 8 可删除(delete)
-- 16 可审核
...
权限的组合
read + insert = 1 + 2 = 3
read + insert + delete = 1 + 2 + 8 = 11
read + update + delete = 1 + 4 + 8 = 13
select 1 | 2 权限的加法 就是逻辑 [ 或 ]运算 -- 结果: 3
select 3 & ( ~ 1) 权限的减法, 使用 [ 与 ]运算 + [ 非 ]运算来实现 -- 结果: 2
select 1 | 13 一次添加n个权限 -- 结果: 13
select 13 & ( ~ 11) 一次减去n个权限 -- 结果:4
select ( 3 & 2) 权限的判断 -- 结果:2
-- 例子
Select * From Customer Where ( @Status is null Or [ Status ] & @Status = [ Status ])
int [] power = new int [] { 1, 2, 4, 8, 16, 32, 64 };
int value = 126;
for ( int i = 0; i < power.Length; i ++)
{
if ((value & power [ i ]) != 0)
{
Console.WriteLine("有power [ {0} ] ={ 1}所代表的权限", i, power [ i ]);
}
}
-- 读取权限
private int [] GetPermission( int PermissionSum)
{
List < int > list = new List < int >();
int [] table = { 1, 2, 4, 8, 16, 32, 64};
for ( int i = table.Length - 1; i >- 1; i -- )
{
if ( table [ i ] == PermissionSum)
{
list. Add( table [ i ]);
break;
}
if ( table [ i ] > PermissionSum)
{
continue;
}
PermissionSum -= table [ i ];
list. Add( table [ i ]);
}
return list.ToArray();
}
http: //www.cnblogs.com /zhuqil /archive / 2010 / 04 / 02 /Permission.html
using System.ComponentModel;
public enum ProjectPhase
{
[Description( " Requirements Gathering ")]
RequirementsGathering = 1,
[Description( " Analysis and Design ")]
AnalysisDesign = 1,
}
// EnumExtensions
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[ 0].Description;
else
return value.ToString();
}
public enum ProjectPhase
{
[Description( " Requirements Gathering ")]
RequirementsGathering = 1,
[Description( " Analysis and Design ")]
AnalysisDesign = 1,
}
// EnumExtensions
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[ 0].Description;
else
return value.ToString();
}
Binding an enumeration to a dropdown list
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
public enum ActiveFrom : int
{
[Description( " Now ")]
DateOfPurchase = 0,
[Description( " Tomorrow ")]
OnLogin = 1,
[Description( " Day After Tomorrow ")]
OnActivation = 2
}
public static class Extensions
{
public static IEnumerable BindWithEnum() where TEnum : struct
{
Type enumType = typeof(TEnum);
Type descriptionAttributeType = typeof(DescriptionAttribute);
List list = new List();
foreach ( int value in Enum.GetValues(enumType))
{
MemberInfo enumObj = enumType.GetMember(Enum.ToObject(enumType, value).ToString()).SingleOrDefault();
object attr = enumObj.GetCustomAttributes(descriptionAttributeType, false).SingleOrDefault();
if (attr == null) continue;
string description = ((DescriptionAttribute)attr).Description;
list.Add( new { Code = value, Name = description });
}
return list;
}
}
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
public enum ActiveFrom : int
{
[Description( " Now ")]
DateOfPurchase = 0,
[Description( " Tomorrow ")]
OnLogin = 1,
[Description( " Day After Tomorrow ")]
OnActivation = 2
}
public static class Extensions
{
public static IEnumerable BindWithEnum() where TEnum : struct
{
Type enumType = typeof(TEnum);
Type descriptionAttributeType = typeof(DescriptionAttribute);
List list = new List();
foreach ( int value in Enum.GetValues(enumType))
{
MemberInfo enumObj = enumType.GetMember(Enum.ToObject(enumType, value).ToString()).SingleOrDefault();
object attr = enumObj.GetCustomAttributes(descriptionAttributeType, false).SingleOrDefault();
if (attr == null) continue;
string description = ((DescriptionAttribute)attr).Description;
list.Add( new { Code = value, Name = description });
}
return list;
}
}
//
Pick a Random Enum in C# (Better than my old post)
static T RandomEnum() {
if ( typeof( T ).IsEnum ) {
var names = Enum.GetNames( typeof( T ) );
return ( T )Enum.Parse( typeof( T ), names[ threadsafeRandom.Value.Next( 0, names.Length ) ] );
}
return default( T );
}
static readonly ThreadLocal threadsafeRandom = new ThreadLocal( () => new Random( RandomSeed() ) );
static T RandomEnum() {
if ( typeof( T ).IsEnum ) {
var names = Enum.GetNames( typeof( T ) );
return ( T )Enum.Parse( typeof( T ), names[ threadsafeRandom.Value.Next( 0, names.Length ) ] );
}
return default( T );
}
static readonly ThreadLocal threadsafeRandom = new ThreadLocal( () => new Random( RandomSeed() ) );
本文转自曾祥展博客园博客,原文链接:
http://www.cnblogs.com/zengxiangzhan/archive/2010/10/06/1844845.html
,如需转载请自行联系原作者