This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24485|Reply: 3

[Source] C# Enum Simple Permission Design uses the FlagsAttribute property

[Copy link]
Posted on 4/22/2019 2:30:51 PM | | | |
In C#, you can perform logical operations on integer objects by bit. The meaning of logical operation by bit is that each bit of the object to be operated is taken in turn, and the logical operation result of each bit is each bit of the result value. The bit logic operators supported by C# are shown in the following figure:



Basic permission design:

///<summary>
///權限列舉
///</summary>
[FlagsAttribute]
publicenumPermissions
{
Description("Not configured")]
None=0,
[Description("Create")]
Create=1,
[Description("Read")]
Read=2,
[Description("Update")]
Update=4,
[Description("Delete")]
Delete=8,
[Description("All Features")]
All=Create| Read| Update| Delete
}



Technique 1: Use the power value of 2 as an enumeration value for future bit operations (&AND, |OR, ^XOR).


Tip 2: Add a new permission action to the existing permission. (OR Operation)

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

Tip 3: Add the [FlagsAttribute] tag

Permissionspermission=Permissions.None;
permission=permission| Permissions.Create;
MessageBox.Show(permission. ToString());
Result:Create([FlagsAttribute] does not affect the result)

permission=permission| Permissions.Read;
MessageBox.Show(permission. ToString());
Result: 3 ([FlagsAttribute] not added)
Result:Create,Read (add [FlagsAttribute])


Tip 4: Remove a certain permission action from the existing permission. (XOR and AND computing applications)

Permissionspermission=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


Tip 5: Determine whether you have a certain permission in the current permission (AND operation)

Permissionspermission=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

boolIsCreatable=false;
if((permission&Permissions.Create)==Permissions.Create)
{
IsCreatable=true;
}
else
{
IsCreatable=false;
}
MessageBox.Show(IsCreatable.ToString());
//Result:True

C# enums use the Flags feature, where multiple values are stored in one enumeration variable
https://www.itsvse.com/thread-4661-1-1.html
(Source: Architect_Programmer)







Previous:System.MissingMethodException: 找不到方法:“Void Dapper.DynamicP...
Next:The similarities and differences between virtual and (abstract) abstracth and interface in C#...
Posted on 4/23/2019 8:28:32 AM |
Discuss carefully and improve together! - Tue Apr 23 2019 08:28:32 GMT+0800 (China Standard Time)
Posted on 11/11/2019 9:49:58 AM |
How do I get a description for a multi-select enumeration?
 Landlord| Posted on 11/11/2019 11:00:19 AM |
Blue Sky and White Clouds Posted on 2019-11-11 09:49
How do I get a description for a multi-select enumeration?

Please refer to the article below

C# Enum enum type operation extension class
https://www.itsvse.com/thread-7286-1-1.html
(Source: Architect_Programmer)
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com