You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can we combine all of those permissions into a single value
[Flags]enumPermissionTypes:int{None=0,Read=1,Write=2,Modify=4,Delete=8Create=16,All=Read|Write|Modify|Delete|Create}//and the class from beforeclassUser{PermissionTypesPermissions=PermissionTypes.None;}
so now what?
//create a new userUseradmin=newUser();admin.Permissions=PermissionTypes.Read|PermissionTypes.Write|PermissionTypes.Delete;//check for permissionsboolcanRead=((PermissionTypes.Read&admin.Permissions)==PermissionTypes.Read);boolcanWrite=((PermissionTypes.Write&admin.Permissions)==PermissionTypes.Write);boolcanCreate=((PermissionTypes.Create&admin.Permissions)==PermissionTypes.Create);//and the resultsConsole.WriteLine(canRead);//trueConsole.WriteLine(canWrite);//trueConsole.WriteLine(canCreate);//false
Lets simplify little more...
namespaceEnum.Extensions{publicstaticclassEnumerationExtensions{//checks if the value contains the provided typepublicstaticboolHas<T>(thisSystem.Enumtype,Tvalue){try{return(((int)(object)type&(int)(object)value)==(int)(object)value);}catch{returnfalse;}}//checks if the value is only the provided typepublicstaticboolIs<T>(thisSystem.Enumtype,Tvalue){try{return(int)(object)type==(int)(object)value;}catch{returnfalse;}}//appends a valuepublicstaticTAdd<T>(thisSystem.Enumtype,Tvalue){try{return(T)(object)(((int)(object)type|(int)(object)value));}catch(Exceptionex){thrownewArgumentException(string.Format("Could not append value from enumerated type '{0}'.",typeof(T).Name),ex);}}//completely removes the valuepublicstaticTRemove<T>(thisSystem.Enumtype,Tvalue){try{return(T)(object)(((int)(object)type&~(int)(object)value));}catch(Exceptionex){thrownewArgumentException(string.Format("Could not remove value from enumerated type '{0}'.",typeof(T).Name),ex);}}}}
and...
//start with a valuePermissionTypespermissions=PermissionTypes.Read|PermissionTypes.Write;//then check for the valuesboolcanRead=permissions.Has(PermissionTypes.Read);//trueboolcanWrite=permissions.Has(PermissionTypes.Write);//trueboolcanDelete=permissions.Has(PermissionTypes.Delete);//false